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.
[System.Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null #parm $delegateControlId = "SmallSearchInputBox" $weburl = "/cwd" $hosturl = "http://win-ugka6ujlm21" $absoluteUrl = $hosturl+$weburl #creating a fake httpcontext $request = New-Object "System.Web.HttpRequest" "", $absoluteUrl, "" $stringWriter = New-Object "System.IO.StringWriter" $httpResponse = New-Object "System.Web.HttpResponse" $stringWriter [System.Web.HttpContext]::Current = New-Object "System.Web.HttpContext" $request, $httpResponse #HACK - Need a 'Microsoft.SharePoint.SPWeb' not a 'System.Management.Automation.PSObject' [System.Web.HttpContext]::Current.Items["HttpHandlerSPWeb"] = (Get-SPSite -Identity $hosturl).OpenWeb($weburl, $false) #create and set properties for the delegate control $dc = New-Object "Microsoft.SharePoint.WebControls.DelegateControl" $dc.AllowMultipleControls = $true $dc.ControlId = $delegateControlId #create a method object and invole the method $bindingFlags = [Reflection.BindingFlags] "NonPublic,Instance" $method = $dc.GetType().GetMethod("CreateChildControls", $bindingFlags) $method.Invoke($dc, @()); #list the child controls $dc.Controls | %{ } #dispose $StringWriter.Dispose | Out-Null $httpResponse.Close() | Out-Null
The code: