Saturday, October 30, 2010

List the controls added to a SPDelegate on a Website

I need to inspect a delegate control, to discover which control there is add to it. So I create this PowerShell code to create a HttpContext, set the right Current.Items to be a real SharePoint context. Create a SharePoint Delegate Control, set the properties and then invoke CreateChildControls on it and out put the result on the screen.

image

  1. [System.Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null
  2. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null
  3.  
  4. #parm
  5. $delegateControlId = "SmallSearchInputBox"
  6. $weburl = "/cwd"
  7. $hosturl = "http://win-ugka6ujlm21"
  8. $absoluteUrl = $hosturl+$weburl
  9.  
  10. #creating a fake httpcontext
  11. $request = New-Object "System.Web.HttpRequest" "", $absoluteUrl, ""
  12. $stringWriter = New-Object "System.IO.StringWriter"
  13. $httpResponse = New-Object "System.Web.HttpResponse" $stringWriter
  14. [System.Web.HttpContext]::Current = New-Object "System.Web.HttpContext" $request, $httpResponse
  15.  
  16. #HACK - Need a 'Microsoft.SharePoint.SPWeb' not a 'System.Management.Automation.PSObject'
  17. [System.Web.HttpContext]::Current.Items["HttpHandlerSPWeb"] = (Get-SPSite -Identity $hosturl).OpenWeb($weburl, $false)
  18.  
  19. #create and set properties for the delegate control
  20. $dc = New-Object "Microsoft.SharePoint.WebControls.DelegateControl"
  21. $dc.AllowMultipleControls = $true
  22. $dc.ControlId = $delegateControlId
  23.  
  24. #create a method object and invole the method
  25. $bindingFlags = [Reflection.BindingFlags] "NonPublic,Instance"
  26. $method = $dc.GetType().GetMethod("CreateChildControls", $bindingFlags)
  27. $method.Invoke($dc, @());
  28.  
  29. #list the child controls
  30. $dc.Controls | %{
  31. $_.GetType().BaseType
  32. }
  33.  
  34. #dispose
  35. $StringWriter.Dispose | Out-Null
  36. $httpResponse.Close() | Out-Null


The code:

No comments:

Post a Comment