SharePoint Wiki Page Operations Using PnP PowerShell

Introduction

In this article, we will learn Wiki page operations on SharePoint, using PnP PowerShell. The Client Side Object Model is used internally for these operations. The operations include, 

  • Adding Wiki page
  • Setting content to Wiki page
  • Retrieve Wiki page
  • Delete Wiki page

Note: I assume a Wiki page library is already created on the SharePoint site, before working on these operations. For my examples, the path to Wiki page library is (http://siteurl/Wiki Pages/Forms/AllPages.aspx)

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

Add New Wiki Page

The Wiki pages can be created on the site, using Add-SPOWikiPage command. The required parameter values for the operation are the server relative page URL and the required page content.

The following code snippet helps to create a Wiki page with the content available on the string variable.

  1. Add-SPOWikiPage -ServerRelativePageUrl "/Wiki Pages/PnPWikiPage1.aspx" -Content "This is test page from pnp"  

The below content shows the content for a new page to be created. Copy and Save it as wikicontent.html.

  1. <div>  
  2. <h2>PnP Wiki Page</h2>  
  3. <br/>  
  4. <div>The page is created by <b>PnP PowerShell command</b></div>  
  5. <br/>  
  6. <div><i>The content is updated from local File</i></div>  
  7.   
  8. </div>  
The code snippet, given below, helps in creating a Wiki page with the content available on the text/html file.
  1. $content = [IO.File]::ReadAllText("D:\Nakkeeran\PnP\wikicontent.html")  
  2. Add-SPOWikiPage -ServerRelativePageUrl "/Wiki Pages/PnPWikiPage2.aspx" -Content $content  
The below snapshot shows the Wiki page created.


Retrieve Wiki Page Content

The content of a particular Wiki page can be retrieved using Get-SPOWikiPageContent command. The required parameter for the operation is the server relative page URL of Wiki page.

The following code snippet helps in retrieving the content of the Wiki page specified.

  1. $pageContent = Get-SPOWikiPageContent -ServerRelativePageUrl "/Wiki Pages/Home.aspx"  

Update Wiki Page Content

Set-SPOWikiPageContent command is used to update the page content.

The Wiki page content can be updated in two ways:

1. Using file content

In this approach, the content for the page is saved in file. Then, the content is updated using the update command and the file path. The following code snippet shows the update operation through path.

  1. Set-SPOWikiPageContent -ServerRelativePageUrl "/Wiki Pages/PnPWikiPage1.aspx" -Path "D:\Nakkeeran\PnP\updatecontent.txt"  

2. Using content string

In this approach, the content is passed through string variable. The following code snippet shows the update operation with string variable.

  1. Set-SPOWikiPageContent -ServerRelativePageUrl "/Wiki Pages/PnPWikiPage2.aspx" -Content "Updated Content with PnP Command"  

Delete Wiki Page

The Wiki page can be deleted using RemoveWikiPage command. The required parameter for delete operation is the server related URL of Wiki page. The following operation deletes the Wiki page.

  1. Remove-SPOWikiPage -ServerRelativePageUrl "/Wiki Pages/PnPWikiPage1.aspx"  

Summary

Thus, you have learned how to carry out the basic Wiki page operations on SharePoint website, programmatically, using PnP PowerShell commands. The operations like creating Wiki page with content, updating Wiki page content, retrieving Wiki page content and deleting Wiki page are explained above. 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.

Up Next
    Ebook Download
    View all
    Learn
    View all