Create Page Using CSOM With PowerShell For SharePoint Online

Introduction

In this article, you will learn how to create a publishing page using CSOM with PowerShell for any SharePoint platform site.

Steps Involved

The following section explains the flow for creating a publishing page. 

  1. Add the references using the Add-Type command with necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll and Microsoft.SharePoint.Client.Publishing.dll.
    1. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
    2. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
    3. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"  
  2. Initialize client context object with the site URL.
    1. $siteURL = ""  
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
  3. If you are trying to access SharePoint Online site, then you need to setup the site credentials with credentials parameter and get it set to the client context. 
    1. #Not required for on premise site - Start  
    2. $userId = ""  
    3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
    5. $ctx.credentials = $creds   
    6. #Not required for on premise site - End  
  4. If you are trying to access the SharePoint on premise site, then the credentials parameter is not required to be set to the context. But you need to run the code on the respective SharePoint server.

  5. Then check if the page already exists. 

    • Get the page library using GetByTitle method from the list collection. 
    • Then initialize the query object and set the query using view xml method. 
    • Get the items using GetItems method. 
    • Load and execute the query. 
    • Then check the items count.
      1. $pageName = "TestPage1.aspx"  
      2. $lists = $ctx.web.Lists   
      3. $list = $lists.GetByTitle("Pages")  
      4. $query = New-Object Microsoft.SharePoint.Client.CamlQuery  
      5. $query.ViewXml = "<View><Query><Where><Contains><FieldRef Name='FileLeafRef' /><Value Type='Text'>"+$pageName+"</Value></Contains></Where></Query></View>"  
      6. $listItems = $list.GetItems($query)  
      7. $ctx.load($listItems)      
      8. $ctx.executeQuery()  
      9.   
      10. if($listItems.Count -le 0){  
      11.     Write-Host "Page doesn't exists! Further Code goes here"  
      12. }  
  6. If the page is not found, we can create it using the existing page layout.

  7. By default page layouts are present in master pages gallery. 

    • Get the page layout library and filter out page layout item using the query object. 
    • Once the query is set, get the necessary page layout using GetItems method. 
    • Since, it returns the item collection, we will take the first item from the collection.
      1. $pageLayoutList = $ctx.web.Lists.GetByTitle("Master Page Gallery")  
      2. $pageLayoutQuery = New-Object Microsoft.SharePoint.Client.CamlQuery  
      3. $pageLayoutQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>BlankWebPartPage.aspx</Value></Eq></Where></Query></View>"  
      4. $pageLayouts = $pageLayoutList.GetItems($pageLayoutQuery)  
      5. $ctx.Load($pageLayouts)  
      6. $ctx.ExecuteQuery()  
      7. $pageLayout = $pageLayouts[0]  
  8. Get the publishing web by loading the existing context and the web context.
    1. #Get the publishing Web  
    2. $pubWeb = [Microsoft.SharePoint.Client.Publishing.PublishingWeb]::GetPublishingWeb($ctx, $ctx.Web)  
    3. $ctx.Load($pubWeb)  
  9. For creating page, 

    • Set the necessary page information using PublishingPageInformation object. The necessary parameters are page name and the page layout item. 

    • Once parameters are set, add the page object using AddPublishingPage method with publishing web. 

    • Then load and execute the query.
      1. $pageInfo = New-Object Microsoft.SharePoint.Client.Publishing.PublishingPageInformation  
      2. $pageInfo.Name = $pageName  
      3. $pageInfo.PageLayoutListItem = $pageLayout  
      4. $page = $pubWeb.AddPublishingPage($pageInfo)  
      5. $ctx.Load($page)  
      6. $ctx.ExecuteQuery()  

The page will be created and present under the pages library. By default, all the publishing pages will be present in the pages library. 

Summary:

Thus you have learnt how to create a publishing page on any SharePoint platform sites using CSOM with PowerShell commands.