Thursday, August 17, 2017

List all UserCustomActions in Sharepoint site collections and sub sites via PowerShell

User custom actions (see SPSite.UserCustomActions and SPWeb.UserCustomActions) are powerful mechanism to add customizations on Sharepoint site (on-premise or online) via javascript. E.g. in one of the previous posts I showed how to add custom javascript file to all pages in your site collection without modifying master page: see Add custom javascript file to all pages in on-premise Sharepoint site collection without modifying masterpage and Add custom javascript file to all pages in Sharepoint Online site collection. Sometimes we need to perform inventory of all custom actions with script links. Here is the PowerShell script which iterates through all site collections in provided web application and all sub sites and outputs custom action’s ScriptSrc to the log file:

   1: param(
   2:     [string]$url
   3: )
   4:  
   5: if (-not $url)
   6: {
   7:     Write-Host "Specify web application url in url parameter"
   8: -foregroundcolor red
   9:     return
  10: }
  11:  
  12: function CheckWeb($web)
  13: {
  14:     Write-Host "Web:" $web.Url
  15:     foreach($ac in $web.UserCustomActions)
  16:     {
  17:         ("  " + $ac.ScriptSrc) | Out-File "log.txt" -Append
  18:     }
  19:     
  20:     $web.Webs | ForEach-Object { CheckWeb $_ }
  21: }
  22:  
  23: function CheckSite($site)
  24: {
  25:     Write-Host "Site collection:" $site.Url
  26:     ("Site collection: " +  $site.Url) | Out-File "log.txt" -Append
  27:  
  28:     foreach($ac in $site.UserCustomActions)
  29:     {
  30:         ("  " + $ac.ScriptSrc) | Out-File "log.txt" -Append
  31:     }
  32:     
  33:     CheckWeb $site.RootWeb
  34:     
  35:     ("---------------------------------------") | Out-File "log.txt" -Append
  36: }
  37:  
  38:  
  39: $wa = Get-SPWebApplication $url
  40: $wa.Sites | ForEach-Object { CheckSite $_ }

Here in order to write results to the log file I used approach described in the following post: Write output to the file in PowerShell. And it is quite straightforward to rewrite this script for Sharepoint Online (see article provided above for Sharepoint Online). Hope it will help someone.

No comments:

Post a Comment