kwizcom banner advertisement

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 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. Just change the Web Application URL from "http://sharepoint.crescent.com" to yours.

SharePoint Web Part Usage Report using PowerShell:
[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 Closed 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 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)
        {
          #
        }
SharePoint Web Part Usage Report - Find All Locations where a Particular web part is being Used SharePoint Web Part Usage Report - Find All Locations where a Particular web part is being Used Reviewed by Salaudeen Rajack on March 12, 2014 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.