Read the web part properties from page using Power Shell
-
08-02-2021 - |
Question
I have on prime SharePoint 2016 site which has couple of web part on the homepage.I want to read the web part properties from the page and Export the data to CSV file using Power Shell commands.
Solution
You can use the below PnP command :
Get-PnPWebPartProperty -ServerRelativePageUrl /sites/demo/sitepages/home.aspx -Identity ccd2c98a-c9ae-483b-ae72-19992d583914 | Export-csv "file path"
Notes:
- In the above, you just can pass your home page URL from where you want to extract the web part property.
Reference :
Using PowerShell CSOM:
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
$siteURL = ""
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
#Not required for on premise site - Start
$userId = ""
$pwd = Read-Host -Prompt "Enter password" -AsSecureString
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
$ctx.credentials = $creds
#Not required for on premise site - End
#page to be viewed
$pageName = "TestPage1.aspx"
#Get the page
$file = $ctx.Web.GetFileByServerRelativeUrl("/Pages/TestPage1.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()
$propValues = $webpart.WebPart.Properties.FieldValues
Write-Host "ID: $webpart.id"
foreach($property in $propValues){
Write-Host "Title: " $property.Title
Write-Host "Description: " $property.Description
Write-Host "Chrome Type: " $property.ChromeType
}
}
}
Reference :
Retrieve Webparts From Page Using CSOM With PowerShell On SharePoint
How to export the report into the CSV file?
if($webparts.Count -gt 0){
Write-Host "Looping through all webparts"
foreach($webpart in $webparts)
{
$ctx.Load($webpart.WebPart.Properties)
$ctx.ExecuteQuery()
$propValues = $webpart.WebPart.Properties.FieldValues
Write-Host "ID: $webpart.id"
foreach($property in $propValues){
Write-Host "Title: " $property.Title
Write-Host "Description: " $property.Description
Write-Host "Chrome Type: " $property.ChromeType
$listItemData = @{
"Webpart Title" = $property.Title
"Webpart Description" = $property.Description
"Chrome Type" = $property.ChromeType
}
New-Object PSObject -Property $listItemData
}
}
}
$listItemData |Export-Csv -NoTypeInformation -Path "Your CSV file path"
To export in CSV refer to the below article:
Get document library inventory report in SharePoint using PowerShell script
OTHER TIPS
After exporting i am getting like this.. But this is not correct way.
Chrome Type: 0
Webpart Title Chrome Type Webpart Description
------------- ----------- -------------------
Web Part Page Title Bar 2
Content Editor 0 Allows authors to enter rich text content.
Script Editor 2 Allows authors to insert HTML snippets or scripts.
Image Viewer 0 Displays a specified image.
PS C:\>
PS C:\> $listItemData |Export-Csv -NoTypeInformation -Path "C:\Users\`enter code here`"
PS C:\>
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange