SharePoint Page Operations Using PnP PowerShell

Introduction

In this article, we will learn how to create, retrieve and delete publishing pages on SharePoint, using PnP PowerShell. The Client Side Object Model is used internally for these operations.

Note: To work with publishing features on SharePoint, the publishing features should be enabled on the site. The features that need to be activated are SharePoint Server Publishing Infrastructure on site collection (site scope) and SharePoint Server Publishing on site (Web scope). Once these features are activated; the pages library will be created on the corresponding site. The URL of the pages library will be http://siteurl/Pages/Forms/AllItems.aspx. All the publishing pages will be available in the pages library.

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 site. 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. $siteurl = "https://abc.sharepoint.com"  
  2. Connect-SPOnline -Url $siteurl  
  3. $ctx = Get-SPOContext  

Create Page

The pages can be created on the site, using Add-SPOPublishingPage command. The required parameters for the operation are the page name and template name. The template name is File Leaf Ref parameter without the extension. Other optional parameters can be the title and publish. The operation can be carried out in multiple ways.

If you know the template name, you can pass it and create the page. The following code helps in creating a page with the known template name.

  1. Add-SPOPublishingPage -PageName "PnPPage1" -PageTemplateName "BlankWebPartPage" -Title "PnPPage1" -Publish  

The other way is to retrieve the page template details, using the title or some other property and creating a page. The following code snippet shows the operation:

  1. $pageLayoutItem = Get-SPOListItem -List "Master Page Gallery" -Query "<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>Blank Web Part Page</Value></Contains></Where></Query></View>"  
  2. $pageTemplateName = $pageLayoutItem.FieldValues["FileLeafRef"]  
  3. $pageTemplateName = $pageTemplateName.Substring(0, $pageTemplateName.IndexOf("."))  
  4. Add-SPOPublishingPage -PageName "PnPPage2" -PageTemplateName $pageTemplateName -Title "PnPPage2" -Publish  

The other operations are carried out with the PnP list item operations. Let’s see the operations here:

Retrieve Pages

The list item command is used to retrieve the pages from the pages library. The following command shows retrieval of all the pages:

  1. $pages = Get-SPOListItem -List "Pages"  
  2. foreach($page in $pages){  
  3.     Write-Host "Title   : " $page["Title"]  
  4.     Write-Host "Page URL: " $page["FileRef"]  
  5.     Write-Host "---------------------------------"  
  6. }  

The following command shows retrieval of a particular page, using the query:

  1. $page = Get-SPOListItem -List "Pages" -Query "<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>PnPPage2</Value></Contains></Where></Query></View>"   
  2. Write-Host "Title   : " $page["Title"]  
  3. Write-Host "Page URL: " $page["FileRef"]  

Other properties can be accessed, using $page.FieldValues.

Delete Page

The list item operation command is used in deleting a page. The command used is Remove-SPOListItem. The required parameters for deleting are page library name (list) and identity. The identity value is a page object. To set the identity, first retrieve the page from the page library, using Get-SPOListItem command.

The following code snippet shows the operation:

  1. $page = Get-SPOListItem -List "Pages" -Query "<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>PnPPage2</Value></Contains></Where></Query></View>"   
  2. Remove-SPOListItem -List Pages -Identity $page  

Summary

Thus, you have learned how to create a page, retrieve a page and delete the page on SharePoint sites programmatically, using PnP PowerShell commands. 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.