Introduction
In this article, we will learn how to retrieve the Web parts and their properties from SharePoint pages on SharePoint sites, using PnP PowerShell. The Client Side Object Model is used internally for these operations.
The Web parts from any SharePoint Server page (Publishing, Wiki or Site Pages) can be accessed on the sites.
Prerequisite
You need to have PowerShell 3.0, available on a Windows machine. You need to install or import PnP PowerShell packages. You can download the installers or view more documentation from the official Website. The installers are available here. Online version installer is preferred for On Premise or Office 365 operations. You can also install all the three installers for testing (SharePoint 2013, 2016, online).
The PnP PowerShell is supported by SharePoint 2013, SharePoint 2016 On Premises and Office 365 versions. The following operations are tested on SharePoint 2013 and Office 365 environments.
Connect To Site
Connect to the site, using the snippet given below. PnP PowerShell code, given below, helps in getting the current context of the site, using the Client Side Object Model (CSOM).
# Get Current Context Site (Root)
$siteurl = "https://abc.sharepoint.com"
Connect-SPOnline -Url $siteurl
$ctx = Get-SPOContext
There are multiple ways to retrieve the Web parts and the properties.
Retrieve Web Parts
The Web parts from the page can be easily accessed, using Get-SPOWebPart command with PnP PowerShell.
The required parameter for accessing all the Web parts is the Server relative URL of page.
The basic properties like title and Id of Web part and Web part zone detail can be accessed from $Webpart object (Object created for the command, given above).
All other properties can be accessed from the Web part properties object $Webpart.WebPart.Properties.FieldValues.
The following code snippet shows how to access the Web parts and their properties.
$webparts = Get-SPOWebPart -ServerRelativePageUrl "/Pages/PnPPage.aspx"
# if there are more than 1 webparts
foreach($webpart in $webparts){
Write-Host "Zone " $webpart.ZoneId
Write-Host "WebPart Id " $webpart.Id
Write-Host "Title " $webpart.WebPart.Title
# Other Web Part Properties can be accessed
$webpartProps = $webpart.WebPart.Properties.FieldValues
foreach($webpartProp in $webpartProps)
{
# Properties of content editor web part
Write-Host "Height : " $webpartProp["Height"]
Write-Host "Width : " $webpartProp["Width"]
Write-Host "ContentLink : " $webpartProp["ContentLink"]
# Other properties can be accessed
}
Write-Host "-------------------------------------------"
}
The code snippet given above shows an example for retrieving the content editor Web part properties. Similarly, other Web part properties can be accessed.
Retrieve Single Web Part
To access the specific Web part, additional parameter identity needs to be passed for the above section's command.
The following code snippet shows how to access the Web part and its properties, using PnP PowerShell.
$webpart = Get-SPOWebPart -ServerRelativePageUrl "/Pages/PnPPage.aspx" -Identity "SPDocuments"
Write-Host "Zone " $webpart.ZoneId
Write-Host "WebPart Id " $webpart. Id
Write-Host "Title " $webpart.WebPart.Title
# Other Web Part Properties can be accessed
$webpartProps = $webpart.WebPart.Properties.FieldValues
foreach($webpartProp in $webpartProps){
Write-Host "Definition : " $webpartProp["XmlDefinition"]
# Other properties can be accessed
}
Retrieve Web Part XML
The Web parts present on SharePoint pages are basically XML files, which have .Webpart or .dwp extensions. The file contains the necessary information of the Web parts in the XML structure.
Here, we will see how to retrieve the Web part XML from the page. The required parameters are Server relative URL of the page and the identity (Web part title).
The code snippet, given below, shows how to retrieve the Web part as XML from the page.
$webpartXML = Get-SPOWebPartXml -ServerRelativePageUrl "/Pages/PnPPage.aspx" -Identity "Test WP"
Retrieve Specific Properties
The Web part properties can also be retrieved, using Get-SPOWebPartProperty command.
The required parameters are Server relative URL of the page, identity (Web part Id).
The optional parameter is the key.
The code snippet, given below, retrieves all the properties of the Web part.
$webpart = Get-SPOWebPart -ServerRelativePageUrl "/Pages/PnPPage.aspx" -Identity "Content Editor"
$webpartProps = Get-SPOWebPartProperty -ServerRelativePageUrl "/Pages/PnPPage.aspx" -Identity $webpart. Id
foreach($webpartProp in $webpartProps){
Write-Host $webpartProp.Key " - " $webpartProp.Value
}
The code snippet, given below, retrieves the specific properties of the Web part using key (property name).
Get-SPOWebPartProperty -ServerRelativePageUrl "/Pages/PnPPage.aspx" -Identity $webpart. Id -Key "Description"
Summary
Thus, you have learned, how to retrieve the Web parts, Web part XML and Web part properties from SharePoint pages on a SharePoint Website programmatically, using PnP PowerShell commands. The operation mentioned above, supports accessing the Web parts from any custom or OOB page. PnP PowerShell is supported by SharePoint 2013, SharePoint 2016 On Premises and Office 365 versions. The operations, mentioned above, are tested on SharePoint 2013 and Office 365 environments.
Comments