Freigeben über


List all CustomActions in the Farm

Here’s a great little script that I used to list out all of the Custom Actions available in a farm.  What’s great about the script is that it returns the CustomActions in XML so you can vary the output.  My favorite is using the Out-GridView cmdlet in Windows PowerShell 2.0. From there can you can do searches, sorts, filters, etc.

Calling the Script

PS C:\>  [xml]$ca = Get-SPCustomAction   

PS C:\>  $ca.CustomActions.CustomAction | Out-GridView

The Function

function Get-SPCustomAction
{  
    trap [Exception] { continue; }
    cls
    $null = [system.reflection.assembly]::loadwithpartialname("Microsoft.Sharepoint")
    $null = [system.reflection.assembly]::loadwithpartialname("Microsoft.Sharepoint.Administration")
    $null = [system.reflection.assembly]::loadwithpartialname("System.Web")
    [string]$xmlString = "<?xml version=""1.0"" encoding=""utf-8"" ?><CustomActions>"
    $farm = [microsoft.sharepoint.administration.spfarm]::local
    foreach ($featureDef in $farm.FeatureDefinitions)
    {
        trap [Exception] { continue; }
        foreach ($elementDef in $featureDef.GetElementDefinitions([System.Globalization.CultureInfo]::CurrentCulture))
        {
            trap [Exception] { continue; }
            $node = $elementDef.XmlDefinition;
            if ($node.Name -eq "CustomAction")
            {
                trap [Exception] { write-error $($_.Exception.Message); continue; }
                $xmlString += "<CustomAction Id=""" + $node.Id + """ GroupId=""" + $node.GroupId + """ Location=""" + $node.Location + """ Sequence=""" + $node.Sequence + """ Title=""" + $node.Title + """ "
                if($node.UrlAction -ne $null -and $node.UrlAction.OuterXml -ne "" -and $node.UrlAction.Url -ne $null -and $node.UrlAction.Url -ne "")
                {
                    $xmlString +=  "Url=""" + [System.Web.HttpUtility]::UrlEncode($node.UrlAction.Url.ToString()) + """ "
                }
                $xmlString += "
xmlns=""https://schemas.microsoft.com/sharepoint/""/>"
            }
        }
    } 

    $xmlString += "</CustomActions>"

    #$xmlString = $xmlString.Replace("&", " ")
    Write-Output  $xmlString
}

Then you can filter down to a “Location” and do a partial name “Id” search for what you might be looking for. 

CustomActionsGridView