Accessing Web Parts From SharePoint Pages Using PnP PowerShell

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).

  1. #Get Current Context Site (Root)  
  2. $siteurl = "https://abc.sharepoint.com"  
  3. Connect-SPOnline -Url $siteurl  
  4. $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.

  1. $webparts = Get-SPOWebPart -ServerRelativePageUrl "/Pages/PnPPage.aspx"  
  2.  
  3. # if there are more than 1 webparts  
  4. foreach($webpart in $webparts){  
  5.       
  6.     Write-Host "Zone " $webpart.ZoneId  
  7.     Write-Host "WebPart Id " $webpart.Id  
  8.   
  9.     Write-Host "Title " $webpart.WebPart.Title  
  10.  
  11.     # Other Web Part Properties can be accessed  
  12.     $webpartProps = $webpart.WebPart.Properties.FieldValues  
  13.     foreach($webpartProp in $webpartProps){  
  14.         # Properties of content editor web part
  15.         Write-Host "Height : " $webpartProp["Height"]  
  16.         Write-Host "Width : " $webpartProp["Width"]  
  17.         Write-Host "ContentLink : " $webpartProp["ContentLink"]  
  18.  
  19.         # Other properties can be accessed  
  20.     }  
  21.     Write-Host "-------------------------------------------"  
  22.   
  23. }  

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.

  1. $webpart = Get-SPOWebPart -ServerRelativePageUrl "/Pages/PnPPage.aspx" -Identity "SPDocuments"  
  2. Write-Host "Zone " $webpart.ZoneId  
  3. Write-Host "WebPart Id " $webpart.Id  
  4.   
  5. Write-Host "Title " $webpart.WebPart.Title  
  6.  
  7. # Other Web Part Properties can be accessed  
  8. $webpartProps = $webpart.WebPart.Properties.FieldValues  
  9. foreach($webpartProp in $webpartProps){  
  10.       
  11.     Write-Host "Definition : " $webpartProp["XmlDefinition"]  
  12.  
  13.     # Other properties can be accessed  
  14. }  

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.

  1. $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.
  1. $webpart = Get-SPOWebPart -ServerRelativePageUrl "/Pages/PnPPage.aspx" -Identity "Content Editor"  
  2. $webpartProps = Get-SPOWebPartProperty -ServerRelativePageUrl "/Pages/PnPPage.aspx" -Identity $webpart.Id  
  3. foreach($webpartProp in $webpartProps){  
  4.     Write-Host $webpartProp.Key " - " $webpartProp.Value  
  5. }  
The code snippet, given below, retrieves the specific properties of the Web part using key (property name). 
  1. 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.