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):
#Get Current Context Site
$siteurl = "https://abc.sharepoint.com"
Connect-SPOnline -Url $siteurl
$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).
Referring 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.
<?xml version="1.0" encoding="utf-8"?>
<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">
<Title>Content Editor</Title>
<FrameType>Default</FrameType>
<Description>Allows authors to enter rich text content.</Description>
<IsIncluded>true</IsIncluded>
<ZoneID>WebPartZone2</ZoneID>
<PartOrder>0</PartOrder>
<FrameState>Normal</FrameState>
<Height />
<Width />
<AllowRemove>true</AllowRemove>
<AllowZoneChange>true</AllowZoneChange>
<AllowMinimize>true</AllowMinimize>
<AllowConnect>true</AllowConnect>
<AllowEdit>true</AllowEdit>
<AllowHide>true</AllowHide>
<IsVisible>true</IsVisible>
<DetailLink />
<HelpLink />
<HelpMode>Modeless</HelpMode>
<Dir>Default</Dir>
<PartImageSmall />
<MissingAssembly>Cannot import this Web Part.</MissingAssembly>
<PartImageLarge>/_layouts/15/images/mscontl.gif</PartImageLarge>
<IsIncludedFilter />
<Assembly>Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
<ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
<Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
<PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
</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.
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.
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.
$newWPXML = [IO.File]::ReadAllText("D:\PnP\NewWebPart.dwp")
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.
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.
Comments