kwizcom banner advertisement

Find All Web Parts in Use in a SharePoint Site using PowerShell

Requirement: Generate a report to get the Inventory of all web parts in use in a SharePoint site collection.

PowerShell script to generate web parts in use in a site collection:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration parameters
$SiteURL = "http://intranet.crescent.com"
$ReportOutput="C:\Webparts-in-use.csv" 

$ResultCollection = @()

#Get All Subsites in a site collection and iterate through each
$Site = Get-SPSite $SiteURL
ForEach($Web in $Site.AllWebs)
{
    write-host Processing $Web.URL
    # If the Current Web is Publishing Web
    if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($Web))
    {
        #Get the Publishing Web 
        $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
                  
        #Get the Pages Library
        $PagesLib = $PubWeb.PagesList
     }
     else
     {
        $PagesLib = $Web.Lists["Site Pages"]
     }             
        #Iterate through all Pages  
        foreach ($Page in $PagesLib.Items | Where-Object {$_.Name -match ".aspx"}) 
        {
            $PageURL=$web.site.Url+"/"+$Page.File.URL
            $WebPartManager = $Page.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
                
            #Get All Web Parts data
            foreach ($WebPart in $WebPartManager.WebParts)
            {
                $Result = New-Object PSObject
                $Result | Add-Member -type NoteProperty -name "Site URL" -value $web.Url
                $Result | Add-Member -type NoteProperty -name "Page URL" -value $PageURL
                $Result | Add-Member -type NoteProperty -name "Web Part Title" -value $WebPart.Title
                $Result | Add-Member -type NoteProperty -name "Web Part Type" -value $WebPart.GetType().ToString()

                $ResultCollection += $Result
            }
        }
}
#Export results to CSV
$ResultCollection | Export-csv $ReportOutput -notypeinformation
This script gets all web parts in site collection and generates a report in CSV format.
web part usage report in sharepoint

How to find a specific Web part usage?
Say you want to find all Bamboo web parts in you SharePoint environment. Simple, just change Line#34 to:
 ForEach ($WebPart in $WebPartManager.WebParts | Where-Object { $_.GetType().ToString() -like "*bamboo*"} ) 

Find Web Parts usage at Web Application Level: 
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration parameters
$WebAppURL = "http://intranet.crescent.com"
$ReportOutput="C:\Webparts-in-use.csv" 

$ResultCollection = @()

#Get All Site Collections in the web application
$Sites = Get-SPWebApplication $WebAppURL | Get-SPSite

#Iterate through each Site collection
ForEach($Site in $Sites)
{
    ForEach($Web in $Site.AllWebs)
    {
        write-host Processing $Web.URL
        # If the Current Web is Publishing Web
        if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($Web))
        {
            #Get the Publishing Web 
            $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
                  
            #Get the Pages Library
            $PagesLib = $PubWeb.PagesList
         }
         else
         {
            $PagesLib = $Web.Lists["Site Pages"]
         }             
            #Iterate through all Pages  
            foreach ($Page in $PagesLib.Items | Where-Object {$_.Name -match ".aspx"}) 
            {
                $PageURL=$web.site.Url+"/"+$Page.File.URL
                $WebPartManager = $Page.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
                
                #Get All Web Parts data
                foreach ($WebPart in $WebPartManager.WebParts)
                {
                    $Result = New-Object PSObject
                    $Result | Add-Member -type NoteProperty -name "Site URL" -value $web.Url
                    $Result | Add-Member -type NoteProperty -name "Page URL" -value $PageURL
                    $Result | Add-Member -type NoteProperty -name "Web Part Title" -value $WebPart.Title
                    $Result | Add-Member -type NoteProperty -name "Web Part Type" -value $WebPart.GetType().ToString()

                    $ResultCollection += $Result
                }
            }
    }
    #Export results to CSV
    $ResultCollection | Export-csv $ReportOutput -notypeinformation -append
} 
Find All Web Parts in Use in a SharePoint Site using PowerShell Find All Web Parts in Use in a SharePoint Site using PowerShell Reviewed by Salaudeen Rajack on August 27, 2015 Rating: 5

2 comments:

  1. We have SSRS and use a lot of ReportViewerWebParts. unfortunately when ever your script encounters one of these it throws this exception:
    Exception calling "GetLimitedWebPartManager" with "1" argument(s): "Object reference not set to an instance of an object."
    At line:31 char:13
    + $WebPartManager = $Page.File.GetLimitedWebPartManager([System.Web.UI ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NullReferenceException

    and logs the webpart with a null for the name and Microsoft.SharePoint.WebPartPages.ErrorWebPart for the type. Do you know why and can it be fixed?

    ReplyDelete
  2. Hi, how do I do this for an entire web application? I get blank webpart name and webpart type as "Microsoft.SharePoint.WebPartPages.ErrorWebPart" . Please help.

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.