Adding Web Parts To SharePoint Pages Using Pnp Powershell

In this article, you will learn how to add the Web parts to SharePoint pages on SharePoint sites, using PnP PowerShell.

Introduction

In this article, we will learn how to add the Web parts to SharePoint pages on SharePoint sites, using PnP PowerShell. The Client Side Object Model is used internally for these operations.

The Web parts, present on the site, can be added to any SharePoint Server page (Publishing, Wiki or Site Pages) 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  
  2. $siteurl = "https://abc.sharepoint.com"  
  3. Connect-SPOnline -Url $siteurl  
  4. $ctx = Get-SPOContext  

Add Web Parts to Pages

There are two ways of adding a Web part to a page (site page or publishing page).

Refering Local File:

The Web parts, available on the SharePoint site, can be added to SharePoint pages, using PnP PowerShell. Add-SPOWebPartToWebPartPage command is used to add the Web parts. The required parameters are: 
  • Server relative URL of the page.
  • Path of the Web part XML file or XML data.
  • Zone Id.
  • Zone index. 

The corresponding Web part XML should be built before adding a Web part to the page. The XML structure differs for each Web part types (OOB/Custom/Content Editor/etc).

For example, following content shows the Web part XML for the content editor Web part.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <WebPart xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/WebPart/v2">  
  3.     <Title>Content Editor</Title>  
  4.     <FrameType>Default</FrameType>  
  5.     <Description>Allows authors to enter rich text content.</Description>  
  6.     <IsIncluded>true</IsIncluded>  
  7.     <ZoneID>WebPartZone2</ZoneID>  
  8.     <PartOrder>0</PartOrder>  
  9.     <FrameState>Normal</FrameState>  
  10.     <Height />  
  11.     <Width />  
  12.     <AllowRemove>true</AllowRemove>  
  13.     <AllowZoneChange>true</AllowZoneChange>  
  14.     <AllowMinimize>true</AllowMinimize>  
  15.     <AllowConnect>true</AllowConnect>  
  16.     <AllowEdit>true</AllowEdit>  
  17.     <AllowHide>true</AllowHide>  
  18.     <IsVisible>true</IsVisible>  
  19.     <DetailLink />  
  20.     <HelpLink />  
  21.     <HelpMode>Modeless</HelpMode>  
  22.     <Dir>Default</Dir>  
  23.     <PartImageSmall />  
  24.     <MissingAssembly>Cannot import this Web Part.</MissingAssembly>  
  25.     <PartImageLarge>/_layouts/15/images/mscontl.gif</PartImageLarge>  
  26.     <IsIncludedFilter />  
  27.     <Assembly>Microsoft.SharePoint, Version=16.0.0.0, Culture=neutralPublicKeyToken=71e9bce111e9429c</Assembly>  
  28.     <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>  
  29.     <ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />  
  30.     <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />  
  31.     <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" /> 
  32. </WebPart>  

 

Note: Similarly, other Web part XML can also be built. To get the XML structure of available Web part on the site, try adding the Web part to page and exporting manually. The corresponding XML file will be downloaded. 

The code snippet, given below, shows adding the Web part to the page, using the Web part XML file, which is saved locally. The extension for the Web part XML file is dwp.
  1. Add-SPOWebPartToWebPartPage -ServerRelativePageUrl "/Pages/PnPPage.aspx" -Path "D:\PnP\NewWebPart.dwp" -ZoneId "WebPartZone2" -ZoneIndex 0  

The code snippet, given below, shows adding the Web part to the site page.

  1. Add-SPOWebPartToWebPartPage -ServerRelativePageUrl "/SitePages/Test.aspx" -Path "D:\PnP\NewWebPart.dwp" -ZoneId "WebPartZone2" -ZoneIndex 0  

By Loading XML data

If XML data is present on a string variable, the content can be used in adding the Web part. For example, we will see how we can read XML content present on the Web part file and then create, using the content directly. 

  • Using ReadAllText method, load XML data from the local file to the variable.
  • Using Add-SPOWebPartToWebPartPage command, add the Web part to the page, using XML parameter and the other default parameters.

The code snippet, given below, shows the operation.

  1. $newWPXML = [IO.File]::ReadAllText("D:\PnP\NewWebPart.dwp")  
  2. Add-SPOWebPartToWebPartPage -ServerRelativePageUrl "/SitePages/Test.aspx" -Xml $newWPXML -ZoneId "WebPartZone2" -ZoneIndex 0  

Wiki Pages

For wiki pages, there is a separate command for adding the Web parts. Add-SPOWebPartToWikiPage command is used to add the Web parts to Wiki Pages. The required parameters are: 

  • Server relative URL of the page.
  • Path of the Web part XML file or XML data.
  • Row.
  • Column

Here, row and column denotes the order by which the Web parts are added. The code snippet, given below, shows adding the Web part to Wiki page.

  1. Add-SPOWebPartToWikiPage -ServerRelativePageUrl "/SitePages/Wiki.aspx" -Path "D:\PnP\NewWebPart.dwp" -Row 1 -Column 1   

Summary

Thus, you have learned how to add the Web parts, using the local file path and with XML variable to any SharePoint pages on SharePoint Website programmatically, using PnP PowerShell commands. The operation mentioned above supports adding the Web parts to 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.