Fetch webpart type for SharePioint classic pages using PnP powershell

Azura 5 Reputation points
2023-03-29T12:44:31.0633333+00:00

Hi,

I have SharePoint online sites with pages in classic mode. I want to get the webparts from all such pages. I am able to fetch all webpart properties using below piece of script except webpart type. I tried to use this command to get it but no luck "WebPart Type" = $Webpart.WebPart.GetType().Name.ToString()
I am not allowed to use CSOM PowerShell in my organization.
Any help here is much appreciated.
Thank you !

$SiteURL = ""
$PageServerRelativeURL = ""
$WebPartsData = @()
$ReportFile = "C:\Webparts.csv"
$Credentials = Get-Credential

try {
	Connect-PnPOnline -Url $SiteURL -Credentials $Credentials -WarningAction Ignore
	$Webparts = Get-PnPWebPart -ServerRelativePageUrl $PageServerRelativeURL

	ForEach($Webpart in $Webparts){
		$WebPartsData += New-Object PSObject -Property @{
			"Site URL" = $SiteURL
			"PageUrl" = $PageServerRelativeURL
			"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
		}
	}
}
catch{
	Write-host -f Red "Error:" $_.Exception
}
$WebPartsData | Export-Csv $ReportFile -NoTypeInformation
Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-03-30T02:45:04.22+00:00

    Hi @Azura ,

    You can use Get-PnPWebPartProperty to retrieve webpart properties.

    Get-PnPWebPartProperty -ServerRelativePageUrl <String> -Identity <Guid> [-Key <String>]
     [-Connection <PnPConnection>] [<CommonParameters>]
    
    

    Please refer to the document

    https://pnp.github.io/powershell/cmdlets/Get-PnPWebPartProperty.html


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.