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:
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:
Find Web Parts usage at Web Application Level:
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 -notypeinformationThis script gets all web parts in 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 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 }
We have SSRS and use a lot of ReportViewerWebParts. unfortunately when ever your script encounters one of these it throws this exception:
ReplyDeleteException 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?
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.
ReplyDeleteis 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
ReplyDeleteUse: Get Web Parts from SharePoint Online page using PowerShell
DeleteHi, 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.
ReplyDelete$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
}
}