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?

Are you looking for a way to get a list of web parts from a modern page in SharePoint Online for troubleshooting or for gathering data about specific web parts on pages? Well, In this post, we’ll show you how to use PowerShell to get web parts from a page. Will also share the PowerShell script to find all web parts inventory from all site pages in your SharePoint Online site.

To get all web parts from a SharePoint Online page 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://Crescent.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, as:

#Parameters
$SiteURL = "https://Crescent.sharepoint.com/sites/Vendors"
$PageServerRelativeURL = "/sites/vendors/SitePages/about-us.aspx"

#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive

#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:

#Parameters
$SiteURL = "https://Crescent.sharepoint.com/sites/vendors"

#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive

#get the page
$Page =  Get-PnPClientSidePage -Identity "Home.aspx"

#Get webparts from modern page
$WebParts = $Page.Controls

#Get web part Title and InstanceID
$Webparts | Select Title,InstanceId

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://Crescent.sharepoint.com/sites/vendors"
$CSVPath = "C:\Temp\Webparts.csv"
$WebPartsData = @()

#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive

#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

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply