Búsqueda de sitios web

SharePoint Online: busque todas las acciones personalizadas mediante PowerShell


Requisito: Buscar todas las acciones personalizadas en SharePoint Online

PowerShell para obtener todas las acciones personalizadas del usuario en SharePoint Online:

La acción personalizada es un mecanismo poderoso para agregar personalización en SharePoint. Tengo el requisito de preparar el inventario de todas las acciones personalizadas en un sitio en particular, y aquí está el script de PowerShell para buscar acciones personalizadas en un sitio determinado.


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"
 
Try{
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get All Custom Actions
    $UserCustomActions= $Ctx.Web.UserCustomActions
    $Ctx.Load($UserCustomActions)
    $Ctx.ExecuteQuery()

    If($UserCustomActions.count -gt 0)
    {
        ForEach($CustomActions in $UserCustomActions)
        {
            Write-Host -f Green "Custom Action Name: '$($CustomActions.Name)' ID: $($CustomActions.ID)"
        }
    }
    Else
    {
        write-host -f Yellow "No Custom Actions Found!"
    }
}
Catch {
        write-host -f Red "Error Getting Custom Actions!" $_.Exception.Message
}

Este script enumera todas las acciones personalizadas del usuario de la web determinada. Puede cambiar el objeto a sitio para encontrar todas las acciones personalizadas del objeto del sitio. Por ejemplo, $UserCustomActions= $Ctx.Web.UserCustomActions a $UserCustomActions= $Ctx.Sitio.UserCustomActions

PowerShell para buscar todas las acciones personalizadas en la colección de sitios de SharePoint Online:

Cambiemos un poco el script anterior para recorrer todos los subsitios de la colección de sitios determinada para obtener un inventario de todas las acciones personalizadas del usuario para el sitio y los objetos web y exportar nuestros hallazgos a un archivo CSV.


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Get-SPOCustomActions([String]$SiteURL,[String]$Scope)
{  
    Try{
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the Web and Sub Sites
        $Ctx.Load($Ctx.Web)
        $Ctx.Load($Ctx.Web.Webs)
        $Ctx.ExecuteQuery()

        #Get All Custom Actions based on given scope: Site or Web
        If($Scope -eq "Site")
        {
            $UserCustomActions= $Ctx.Site.UserCustomActions
            Write-host -f Yellow "Searching $Scope Scoped Custom Actions at '$SiteURL'"
        }
        ElseIf($Scope -eq "Web")
        {
            $UserCustomActions= $Ctx.Web.UserCustomActions
            Write-host -f Yellow "Searching $Scope Scoped Custom Actions at '$SiteURL"
        }
        $Ctx.Load($UserCustomActions)
        $Ctx.ExecuteQuery()

        $CustomActionsData = @()
        ForEach($CustomAction in $UserCustomActions)
        {
            Write-Host -f Green "Found a Custom Action: '$($CustomAction.Name)' at : $($SiteURL)"
        
            #Get Custom Action Data
            $CustomActionsData += New-Object PSObject -Property @{
                    'Site URL' = $SiteUrl
                    'Custom Action Title' = $CustomAction.Title
                    'Name' = $CustomAction.Name
                    'ID' = $CustomAction.ID
                    'Group' = $CustomAction.Group
                    'Location' = $CustomAction.Location                
                    'Sequence' = $CustomAction.Sequence
                    'Url' = $CustomAction.Url
                    'Scope' = $CustomAction.Scope
                    'ScriptBlock' = $CustomAction.ScriptBlock
                    'ScriptSrc' = $CustomAction.ScriptSrc 
                    }
        }
        #Export the data to CSV
        $CustomActionsData | Export-Csv $ReportOutput -NoTypeInformation -Append
    
        #Get the Root Web's Custom Actions
        If($Scope -eq "Site")
        {         
            #Call the function to get Root Web's Custom Actions
            Get-SPOCustomActions -SiteURL $SiteURL -Scope "Web"   
        }
        Else
        {
            #Iterate through each subsite of the current web
            ForEach ($Subweb in $Ctx.Web.Webs)
            {
                #Call the function recursively 
                Get-SPOCustomActions -SiteURL $Subweb.url -Scope "Web"
            }
        }
    }
    Catch {
        write-host -f Red "Error Generating Custom Actions Report!" $_.Exception.Message
    }
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

$ReportOutput = "C:\Temp\CustomActions.csv"
#Delete the Output Report, if exists
if (Test-Path $ReportOutput) { Remove-Item $ReportOutput }

#Call the function 
Get-SPOCustomActions -SiteURL $SiteURL -Scope "Site" 

Obtener todas las acciones personalizadas usando PnP PowerShell

Para recuperar todas las acciones personalizadas, utilice el cmdlet PnP PowerShell Get-PnPCustomAction


#Variable
$SiteURL = "https://Crescent.sharepoint.com"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get all custom actions
Get-PnPCustomAction -Scope All

Puede establecer el alcance en "Sitio" o "Web" para recuperar la colección de sitios o acciones personalizadas específicas del sitio.

Artículos relacionados