Monday, 27 August 2018

Provisioning Layout and Static Content for Office 365 SharePoint Wiki Pages Using PnP PowerShell

Here let us look how to add text to the wiki pages using text layouts with predefined text. In the samples, let us use PnP PowerShell scripts to create a wiki page, apply default layout and for adding content.

Problem Statement: Programmatically creating or updating wiki pages which uses OOB wiki page layouts with some predefined text. This is easily possible with manual actions. While creating a page using client side scripts, adding content to the page is not feasible. So, let us look how to add the text to the created wiki pages programmatically using PnP PowerShell scripts.

Applying Wiki Page Layouts with Content

The following are the points to be considered for adding text to wiki pages.
  • Create a Wiki Page (This is required if the page doesn’t exist already)
  • Retrieve the wiki page to be updated – Using CAML query to get the required page list item.
  • Apply text layout with predefined text to a retrieved wiki page.
Before getting into operations, let us connect to the SharePoint site using PnP PowerShell using the following snippet.

Create a wiki page: Let us programmatically add a page to a SharePoint site using PnP PowerShell script. The following snippet creates a page with the required layout.
Note: Skip the above step if wiki page is already created.

Retrieve a wiki page: To update a wiki page, corresponding item/page needs to be retrieved. So CAML query is built and the page/list item is retrieved. The following code snippet helps retrieving the page, before updating.

Update wiki page with layout and text: Now we will see how to update layout and text, for the page/item retrieved above. Remember, we have created a wiki page with the layout called “TwoColumnsHeaderFooter”. The corresponding HTML needs to be set to a variable along with the predefined text.

The wiki page layout contains layouts table HTML and layouts data span. The following snippet contains Two Columns with Header and Footer layout html. The span below the table present in wikiContent variable is called layouts span. The span data (header, footer and column = true, true, 3) denotes layout contains header, footer and 3 columns. If the layout is two columns header, then the layouts data will be (true, false, 2).

The following snippet shows the code to update the layout along with text.

Note: If you are not sure of building the default layout HTML, please refer to the existing layouts by changing the layout of the wiki page.

The following snapshot shows the wiki page created (in edit mode).
Wiki Page created with layout and static text using PnP PowerShell script
Wiki Page created with layout and static text using PnP PowerShell script