SharePoint Web Part Usage Report - Find All Locations where a Particular web part is being Used

During Migration, I had to get the URLs where a particular web part is being used. So, Used this PowerShell script to generate the SharePoint web part usage report.

This script made compatible with SharePoint 2007, So it can be used in SharePoint 2010 and in SharePoint 2013 as well. Don't forget to change the Web Application URL from "http://sharepoint.crescent.com" to yours.

SharePoint Web Part Usage Report using PowerShell:
Let's find all instances of "Excel Services" web part in our SharePoint environment. You may have to change the "Where" condition in Line#50 based on your requirements.
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
 
Function global:Get-SPWebApplication($WebAppURL)
{
 return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebAppURL)
}

#Get-SPSite cmdlet for MOSS 2007
function global:Get-SPSite($url)
 {
    return new-Object Microsoft.SharePoint.SPSite($url)
 }

Function global:Get-SPWeb($url)
{
  $site= New-Object Microsoft.SharePoint.SPSite($url)
        if($site -ne $null)
            {
               $web=$site.OpenWeb();       
            }
    return $web
}
 
#Write Header to CSV File
"Page URL,  Web Part Name" | out-file "D:\Data\ExcelServicesWPs.csv"
 
#Get all Webs
$WebApp = Get-SPWebApplication "http://sharepoint.crescent.com" 
ForEach($site in $WebApp.Sites)
{
   #Iterate through webs
   Foreach ($web in $site.AllWebs)
   {
      #Get All Pages from site's Root into $AllPages Array
      $AllPages = @($web.Files | Where-Object {$_.Name -match ".aspx"})
 
      #Search All Folders for Pages
      ForEach ($folder in $web.Folders)
      {
         #Add the pages to $AllPages Array
         $AllPages += @($folder.Files | Where-Object {$_.Name -match ".aspx"})
      }

      #Iterate through all pages
      ForEach($Page in $AllPages)
      {
          $webPartManager = $web.GetLimitedWebPartManager($Page.ServerRelativeUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
    
          # Array to Hold Web Parts
          ForEach ($webPart in $webPartManager.WebParts | Where-Object {$_ -like "*Excel*"})
          {
             $Result = "$($web.site.Url)$($Page.ServerRelativeUrl), $($webpart.Title)"
             Write-Host "Web Part Found at: "$result
             $result | Out-File "D:\Data\ExcelServicesWPs.csv" -Append
          } 
      }
   }
} 
The above script scans all libraries (not just "Site Pages" or "Pages" library! What if a web part page stored in a "documents" library?) from all sites where a particular webpart is being used and generates a report in CSV format.

But if you want to scan only publishing pages, Here is how the script goes:
 
$PublishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$PagesLib = $PublishingWeb.GetPublishingPages()
ForEach($Page in $PagesLib)
{
          #Insert 
}

2 comments:

  1. Brother, I have run most of your scripts, but why does it return empty output file? except the hardcoded text
    What changes i have to apply in the script to get the correct output ??

    ReplyDelete
    Replies
    1. This script checks for "Excel Services" Web parts - You may change the "where" condition in Line#50 accordingly!

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.