[Verified] Get all webpart properties from SharePoint Online site page using the PowerShell script

[Verified] Get all webparts from pages in a site using PowerShell in SharePoint Online

No comments

 1,669 total views,  2 views today

In this tutorial, I will share how we can get all webparts from pages in a site using PowerShell CSOM and get web part properties using PnP PowerShell.

 

Key Highlights

  • Get all webparts from pages in a site using PowerShell CSOM and get web part properties using Powershell
  • Retrieve webparts properties from SharePoint online page using PowerShell Get-PnPWebPartProperty command
  • How to export the SharePoint web part properties into a CSV file using the PowerShell script?

 

Get all webparts from pages in a site using PowerShell CSOM and get web part properties using Powershell

Get all webparts from pages in a site using PowerShell CSOM script can be used to SharePoint Online and SharePoint 2013/2016/2019 environment.

[Verified] Get all webpart properties from SharePoint Online site page using the PowerShell script
Get all webpart properties from the SharePoint Online site page using the PowerShell script

#The below function will read the web part properties from a given SharePoint page which will take siteURL, PageRelativeURL, UserName and Password as paramaters.
#Load SharePoint 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"
cls

$fileName = "WebPart_Properties_Report"
#'yyyyMMddhhmm yyyyMMdd
$enddate = (Get-Date).tostring("yyyyMMddhhmmss")
#$filename = $enddate + '_VMReport.doc'
$logFileName = $fileName +"_"+ $enddate+"_Log.txt"
$invocation = (Get-Variable MyInvocation).Value
$directoryPath = Split-Path $invocation.MyCommand.Path

$directoryPathForLog=$directoryPath+"\"+"LogFiles"
if(!(Test-Path -path $directoryPathForLog))
{
New-Item -ItemType directory -Path $directoryPathForLog
#Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
#$logPath = $directoryPath + "\" + $logFileName

$logPath = $directoryPathForLog + "\" + $logFileName

$isLogFileCreated = $False

#DLL location

$directoryPathForDLL=$directoryPath+"\"+"Dependency Files"
if(!(Test-Path -path $directoryPathForDLL))
{
New-Item -ItemType directory -Path $directoryPathForDLL
#Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}

#DLL location
$clientDLL=$directoryPathForDLL+"\"+"Microsoft.SharePoint.Client.dll"
$clientDLLRuntime=$directoryPathForDLL+"\"+"Microsoft.SharePoint.Client.dll"

Add-Type -Path $clientDLL
Add-Type -Path $clientDLLRuntime
#File Download location

$directoryPathForFileDownloadLocation=$directoryPath+"\"+"Downloaded Files"
if(!(Test-Path -path $directoryPathForFileDownloadLocation))
{
New-Item -ItemType directory -Path $directoryPathForFileDownloadLocation
#Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}

#File Download location

function Write-Log([string]$logMsg)
{
if(!$isLogFileCreated){
Write-Host "Creating Log File..."
if(!(Test-Path -path $directoryPath))
{
Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
else
{
$script:isLogFileCreated = $True
Write-Host "Log File ($logFileName) Created..."
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}
else
{
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}

#The below function will read the web part properties from a given SharePoint page which will take siteURL, PageRelativeURL, UserName and Password as paramaters.
Function GetWebPartPropertyDetails()
{
param
(
[Parameter(Mandatory=$true)] [string] $SPOSiteURL,
[Parameter(Mandatory=$true)] [string] $pageRelativeURL,
[Parameter(Mandatory=$true)] [string] $UserName,
[Parameter(Mandatory=$true)] [string] $Password
)

Try
{

$securePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
#Setup the Context
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SPOSiteURL)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $securePassword)

$fileByServerRelativeUrl=$pageRelativeURL

#Getting the page details
$file = $ctx.Web.GetFileByServerRelativeUrl($fileByServerRelativeUrl)
$ctx.Load($file)
$ctx.ExecuteQuery()

#Extract all the webparts from the given page.
Write-Host "Extracting all webparts from the given page."
$wpManager = $file.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)
$webpartColls = $wpManager.Webparts
$ctx.Load($webpartColls)
$ctx.ExecuteQuery()

if($webpartColls.Count -gt 0)
{
Write-Host "Looping through all webparts in the page."

$webPartCount=1
foreach($oneWebpart in $webpartColls)
{
$ctx.Load($oneWebpart.WebPart.Properties)
$ctx.ExecuteQuery()
$wpPropValues = $oneWebpart.WebPart.Properties.FieldValues
Write-Host "Webpart ID: " $oneWebpart.ID

Write-Host $webPartCount

Write-Host "##########################" -f Green

foreach($oneProperty in $wpPropValues)
{

Write-Host "Title: " $oneProperty.Title
Write-Host "Description: " $oneProperty.Description
Write-Host "Chrome Type: " $oneProperty.ChromeType
Write-Host "Back Ground Color: "$oneProperty.BackgroundColor
Write-Host "Allow Edit: "$oneProperty.AllowEdit
Write-Host "Allow Hide: "$oneProperty.AllowHide

}
Write-Host "##########################" -f Green
$webPartCount++
}
}

Write-host -f Green "The web part details successfully have been extracted."
}
Catch
{

$ErrorMessage = $_.Exception.Message +"in reading web part properties!:"
Write-Host $ErrorMessage -BackgroundColor Red
Write-Log $ErrorMessage

}
}
#Parameters value
$siteURL="https://globalsharepoint2020.sharepoint.com/sites/CustomSearchRND"

#$PageRelativeURL="/SitePages/TestWPPage.aspx"
$PageRelativeURL="/sites/CustomSearchRND/SitePages/TestWPPage.aspx"
$downloadLocation=$directoryPathForFileDownloadLocation;
$userName = "YourSPOUsername"
$passWord = "YourSPOPassword"
#Parameters ends here.

#Calling the GetWebPartPropertyDetails function and passing the parameters.
GetWebPartPropertyDetails $siteURL $PageRelativeURL $userName $passWord

SharePoint Online WebPart Properties FieldValues Table:

We can retrive the below web part properties from the PowerShell CSOM coding:

Key                 Value
Height
Direction 0
ExportMode 1
Width
ChromeState 0
AllowClose False
ChromeType 2
Hidden False
CatalogIconImageUrl
AllowConnect True
AllowEdit True
AllowHide True
HelpUrl
HelpMode 1
TitleIconImageUrl
TitleUrl
AllowMinimize False
Description
Title Web Part Page Title Bar
AllowZoneChange True
AuthorizationFilter
MissingAssembly Cannot import this Web Part.
ImportErrorMessage Cannot import this Web Part.
HeaderDescription
HeaderCaption
HeaderTitle TestWPPage
Image

SharePoint Online WebPart Properties FieldValues screenshot:

SharePoint Online WebPart Properties FieldValues
SharePoint Online WebPart Properties FieldValues

Retrieve webparts from SharePoint page using PowerShell CSOM:

Once, we execute the above PowerShell script we can retrieve the web parts from the SharePoint page using the PowerShell CSOM.

Retrieve webparts from SharePoint page using PowerShell CSOM
Retrieve webparts from SharePoint page using PowerShell CSOM

How to execute the get all webparts from pages in a site using PowerShell CSOM script?

At the end of the script, go to the parameter section and pass the parameters as like below:


#Parameters value
$siteURL="https://globalsharepoint2020.sharepoint.com/sites/CustomSearchRND"

#$PageRelativeURL="/SitePages/TestWPPage.aspx"
$PageRelativeURL="/sites/CustomSearchRND/SitePages/TestWPPage.aspx"
$downloadLocation=$directoryPathForFileDownloadLocation;
$userName = "YourSPOUsername@tenant.domain.com"
$passWord = "YourSPOPassword"
#Parameters ends here.

#Calling the GetWebPartPropertyDetail function and passing the parameters.
GetWebPartPropertyDetails $siteURL $PageRelativeURL $userName $passWord

Get-PnPWebPartProperty: Retrieve webparts properties from SharePoint page using PowerShell Get-PnPWebPartProperty command

Similary, using the Get-PnPWebPartProperty PnP command also we can retrive webparts properties from SharePoint online page.


############################Description#######################################################
##The below script will read the web part properties from a given SharePoint page which will take ServerRelativePageUrl, Webpart ID as paramaters.
##############################################################################################

CLS
$siteURL="https://globalsharepoint2020.sharepoint.com/sites/CustomSearchRND/"
$userName = "YourSPOUsername@tenant.domain.com"
$passWord = "YourSPOPassword"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord

Connect-PnPOnline -Url $siteURL -Credentials $cred

Get-PnPWebPartProperty -ServerRelativePageUrl "/sites/CustomSearchRND/SitePages/TestWPPage.aspx" -Identity "dd21050d-1d5f-4379-9dd2-ca2ef151428d"

Retrieve webparts properties from SharePoint online page using PowerShell Get-PnPWebPartProperty command – screenshot

Retrieve webparts properties from SharePoint online page using PowerShell Get-PnPWebPartProperty command
Retrieve webparts properties from SharePoint online page using PowerShell Get-PnPWebPartProperty command

How to export the SharePoint web part properties into a CSV file using the PowerShell script?

Well, with the little modification to the above code, we can export the SharePoint web part properties into a CSV file using the PowerShell script.


Function GetWebPartPropertyDetails()
{
param
(
[Parameter(Mandatory=$true)] [string] $SPOSiteURL,
[Parameter(Mandatory=$true)] [string] $pageRelativeURL,
[Parameter(Mandatory=$true)] [string] $UserName,
[Parameter(Mandatory=$true)] [string] $Password
)

Try
{

$securePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
#Setup the Context
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SPOSiteURL)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $securePassword)

$fileByServerRelativeUrl=$pageRelativeURL

#Getting the page details
$file = $ctx.Web.GetFileByServerRelativeUrl($fileByServerRelativeUrl)
$ctx.Load($file)
$ctx.ExecuteQuery()
#Extract all the webparts from the given page.
Write-Host "Extracting all webparts from the given page."
$wpManager = $file.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)
$webpartColls = $wpManager.Webparts
$ctx.Load($webpartColls)
$ctx.ExecuteQuery()

if($webpartColls.Count -gt 0)
{
Write-Host "Looping through all webparts in the page."

$webPartCount=1
foreach($oneWebpart in $webpartColls)
{
$ctx.Load($oneWebpart.WebPart.Properties)
$ctx.ExecuteQuery()
$wpPropValues = $oneWebpart.WebPart.Properties.FieldValues
Write-Host "Webpart ID: " $oneWebpart.ID

Write-Host $webPartCount

Write-Host "##########################" -f Green

foreach($oneProperty in $wpPropValues)
{

Write-Host "Title: " $oneProperty.Title
Write-Host "Description: " $oneProperty.Description
Write-Host "Chrome Type: " $oneProperty.ChromeType
Write-Host "Back Ground Color: "$oneProperty.BackgroundColor
Write-Host "Allow Edit: "$oneProperty.AllowEdit
Write-Host "Allow Hide: "$oneProperty.AllowHide
$listItemData = @{
"Webpart Title" = $oneProperty.Title
"Webpart Description" = $oneProperty.Description
"Chrome Type" = $oneProperty.ChromeType
"Allow Edit" = $oneProperty.AllowEdit
"Allow Hide" = $oneProperty.AllowHide
}

New-Object PSObject -Property $listItemData

}
Write-Host "##########################" -f Green
$webPartCount++
}

}

Write-host -f Green "The web part details successfully have been extracted."
}
Catch
{

$ErrorMessage = $_.Exception.Message +"in reading web part properties!:"
Write-Host $ErrorMessage -BackgroundColor Red
Write-Log $ErrorMessage

}

return $listItemData
}

Test the above code – by calling the function:


#Parameters value
$siteURL="Your site URL"

$PageRelativeURL="/sites/yoursite/SitePages/your_webpart_page.aspx"
$userName = "Your username"
$passWord = "Your Password"
#Parameters ends here.

#Calling the GetWebPartPropertyDetail function and passing the parameters.
GetWebPartPropertyDetails $siteURL $PageRelativeURL $userName $passWord | Export-Csv "C:\Temp\TestExport.csv"

Output: exported SharePoint web part properties into a CSV file using the PowerShell script

How to export the SharePoint web part properties into a CSV file using the PowerShell script?
How to export the SharePoint web part properties into a CSV file using the PowerShell script?

Summary:

Thus, in this article, we have learned the below:

  • How to get all webparts from pages in a site using PowerShell CSOM and get web part properties using Powershell CSOM in SharePoint Online.
  • How to retrieve webparts properties from SharePoint online page using PowerShell Get-PnPWebPartProperty command
  • How to export the SharePoint web part properties into a CSV file using the PowerShell script.

You may also like the following SharePoint PowerShell tutorials: