SharePoint Online: Web Part Usage Report using PowerShell
Requirement: Get All Web Parts from SharePoint Online Page using PowerShell
SharePoint Online: How to Find All Web Parts from a Page?
To get all web parts from a SharePoint Online pages using PowerShell CSOM, use:
#Load SharePoint Online CSOM Assemblies Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" #Parameters $SiteURL = "https://crescentintranet.sharepoint.com/sites/Vendors" $PageServerRelativeURL = "/sites/Vendors/SitePages/About-Us.aspx" #Setup Credentials to connect $Cred = Get-Credential #Setup the context $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password) #Get the page $Page = $Ctx.Web.GetFileByServerRelativeUrl($PageServerRelativeURL) $Ctx.Load($Page) $Ctx.ExecuteQuery() #Get all webparts from the page $WPManager = $Page.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared) $WebParts = $WPManager.Webparts $Ctx.Load($WebParts) $Ctx.ExecuteQuery() #Iterate through all webparts in the page ForEach($Webpart in $Webparts) { #Get Web part Properties $WPProperties = $Webpart.WebPart.Properties $Ctx.Load($WPProperties) $Ctx.ExecuteQuery() #Get Web part Properties Write-Host "Webpart ID: " $Webpart.ID Write-host "Title:" $WPProperties.FieldValues.Title Write-Host "Hidden: " $WPProperties.FieldValues.Hidden Write-Host "Allow Hide: " $WPProperties.FieldValues.AllowHide Write-Host "Allow Close: " $WPProperties.FieldValues.AllowClose Write-Host "`n" }
This script gets all web parts from the given page.
PnP PowerShell to Get All Web Parts from a Page
Similarly, we can use PnP PowerShell to find web parts from a web part page.
#Parameters $SiteURL = "https://crescentintranet.sharepoint.com/sites/Vendors" $PageServerRelativeURL = "/sites/vendors/SitePages/about-us.aspx" #Connect to SharePoint Online site Connect-PnPOnline -Url $SiteURL -UseWebLogin #Get All Web parts from the page $Webparts = Get-PnPWebPart -ServerRelativePageUrl $PageServerRelativeURL #Iterate through webparts ForEach($Webpart in $Webparts) { #Get Web part properties Write-Host "WebPart Id:" $Webpart.Id Write-Host "Title:" $Webpart.WebPart.Title Write-Host "Hidden:" $Webpart.WebPart.Hidden Write-Host "Allow Hidden:" $Webpart.WebPart.Properties.FieldValues.AllowHide }
To get web parts from modern pages, use:
Find All Web Parts Usage Inventory and Export to a CSV
#Parameters $SiteURL = "https://crescentintranet.sharepoint.com/sites/vendors" #Connect to SharePoint Online site Connect-PnPOnline -Url $SiteURL -UseWebLogin $Page = Get-PnPClientSidePage -Identity "About.aspx" #Get webparts from modern page $WebParts = $Page.Controls ForEach($Webpart in $Webparts) { Write-Host "WebPart Id:" $webpart.InstanceId Write-Host "Title:" $webpart.Title }
Find All Web Parts Usage Inventory and Export to a CSV
Let's get web parts from all pages on the site and create a CSV report using PnP PowerShell.
#Parameters $SiteURL = "https://crescentintranet.sharepoint.com/sites/vendors" $CSVPath = "C:\Temp\Webparts.csv" $WebPartsData = @() #Connect to SharePoint Online site Connect-PnPOnline -Url $SiteURL -UseWebLogin #Get all pages from "Site Pages" library $SitePages = Get-PnPListItem -List "Site Pages" #Iterate through each page ForEach($Page in $SitePages) { #Get All Web parts from the page $Webparts = Get-PnPWebPart -ServerRelativePageUrl $Page.FieldValues.FileRef #Iterate through webparts and collect details ForEach($Webpart in $Webparts) { #Get Web part properties $WebPartsData += New-Object PSObject -Property @{ "PageUrl" = $Page.FieldValues.FileRef "WebPart ID" = $Webpart.Id "WebPart Title" = $Webpart.WebPart.Title "Is Closed" = $Webpart.WebPart.IsClosed "Hidden" = $Webpart.WebPart.Hidden "Zone Index" = $Webpart.WebPart.ZoneIndex "Allow Hide" = $Webpart.WebPart.Properties.FieldValues.AllowHide } } } #Export Web part data to CSV $WebPartsData $WebPartsData | Export-Csv -Path $CSVPath -NoTypeInformation
No comments:
Please Login and comment to get your questions answered!