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 = "https://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 a site collection and generates a report in CSV format.
How to find a specific Web part usage?
Say you want to find all Bamboo web parts in your 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 = "https://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
}
This only works if you do not have any document libraries that are “Web Part Libraries” other than Site Pages or have not customized any page that is not in “Site Pages” library with web parts. This will not give full results, Also if the site was migrated from an older version of SharePoint and has all of its pages in a Document Library named “Pages” it will not find them neither. Better title for this is Find All Web Parts in Use in a SharePoint Site stored in “Site Pages” or Publishing page list.
Hi, I want to Get “Web Part Type” value in SPO. Please let me know how can i achieve using CSOM Code.in same way i have write code to get “Web Part Type” value but it’s not worked for me. Please see my CSOM Code.
$file = $ctx.Web.GetFileByServerRelativeUrl(“/sites/SharegateTestSite/Pages/Test.aspx”)
$ctx.Load($file)
$ctx.ExecuteQuery()
#Get all the webparts
Write-Host “Retrieving webparts”
$wpManager = $file.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)
$webparts = $wpManager.Webparts
$ctx.Load($webparts)
$ctx.ExecuteQuery()
if($webparts.Count -gt 0){
Write-Host “Looping through all webparts”
foreach($webpart in $webparts){
$ctx.Load($webpart.WebPart.Properties)
$ctx.ExecuteQuery()
Write-Host $webpart.WebPart.Properties.FieldValues.Title
Write-Host $webpart.GetType().Name
}
}
is there a way to get this information for SharePoint Online Modern Pages. I tried, but I’m not able to get webpart type for modern sharepoint pages
Use: Get Web Parts from SharePoint Online page using PowerShell
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.
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?