Skip to content

Instantly share code, notes, and snippets.

@glapointe
Created January 19, 2017 17:27
Show Gist options
  • Save glapointe/545ee4e0d4593bd7461119ae788eba9f to your computer and use it in GitHub Desktop.
Save glapointe/545ee4e0d4593bd7461119ae788eba9f to your computer and use it in GitHub Desktop.
Retrieves the control elements registered for a given delegate control ID.
<#
.Synopsis
Retrieves the control elements registered for a given delegate control ID.
.DESCRIPTION
Retrieves the control elements registered for a given delegate control ID.
.EXAMPLE
Get-SPControlElement -Web "http://demo" -ControlId "QuickLaunchDataSource" -TopOnly
.EXAMPLE
Get-SPControlElement -Web "http://demo" -ControlId "QuickLaunchDataSource" -Scope "Web"
#>
function Get-SPControlElement {
[CmdletBinding()]
Param (
# Specify the URL of an existing Site or an existing SPWeb object.
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromPipeline = $true,
Position = 0)]
[Microsoft.SharePoint.PowerShell.SPWebPipeBind]
$Web,
# The ID of the delegate control to return instances of.
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromPipeline = $false,
Position = 1)]
[string]
$ControlId,
# If specified, query for the top element only. If not specified, show all controls registered for the control ID.
[Parameter(Mandatory = $false,
Position = 2)]
[Switch]
$TopOnly,
# The scope to search for. Valid values are "Farm", "WebApplication", "Site", and "Web". To show controls registered at all scopes omit the parameter or pass in a $null value.
[Parameter(Mandatory = $false,
Position = 3)]
[string]
$Scope
)
Begin {
$bindings = @("InvokeMethod", "NonPublic", "Instance", "CreateInstance", "Public")
$asm = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$type = $asm.GetType("Microsoft.SharePoint.SPElementProvider")
$constructor = $type.GetConstructor($bindings, $null, @(), $null)
$provider = $constructor.Invoke(@())
}
Process {
$spWeb = $Web.Read()
if ($TopOnly) {
$method = $type.GetMethod("QueryForTopControlElement", $bindings)
} else {
$method = $type.GetMethod("QueryForControlElements", $bindings)
}
$method.Invoke($provider, @([Microsoft.SharePoint.SPWeb]$spWeb, $Scope, $ControlId))
$spWeb.Dispose()
}
End {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment