Add Publishing Page On SharePoint Online Site Using PnP Core CSOM Library

Introduction
 
In this article, you will learn how to add a publishing page to SharePoint pages library on SharePoint Online site, using PnP Core CSOM library.
 
The main advantage of using PnP Core libraries is the reduced code to get the required information. The required object can be retrieved with a very small piece of code, once the client context is set.
 
Prerequisite
  • PnP Core CSOM documentation can be found on the official site here.
  • PnP Core CSOM library packages can be downloaded here
The code, given below, is being tested, using Visual Studio console Application. Once the console Application is created, the packages can be installed, using Install-Package SharePointPnPCoreOnline command on Package Manager console of Visual Studio. Once installed, the references and packages will be imported to the solution.
 
The references used in the sample are given below.
  • Microsoft.SharePoint.Client
  • OfficeDevPnP.Core 
Note 

Publishing features should be activated at both site and Web scope levels. Publishing library should be available on the site. 
 
Connect to SharePoint online site
 
The Authentication Manager is used to retrieve the client context of the site. To connect to SharePoint Online site, the token, given below is used.
  • GetSharePointOnlineAuthenticatedContextToken
The parameters required are.
  • SharePoint Online site URL
  • Tenant UserId
  • Tenant Password (or secured string)
Add Publishing Page
 
The pages can be added to the pages library, using PnP Core component. The steps, given below explain the process in detail. 
  • Input the site detail, user details for authentication and page information.
  • Authenticate and get the client context of the site and then the necessary Web object.
  • Using Web object, create the page, using AddPublishingPage method. The required parameters are given below.

    • Page Name
    • Template Name
    • Page Title

  • Other optional parameters are as follows.

    • publish (boolean value) - To be published?
    • folder - Target folder name. If left as blank or null, created under pages library 
    • start date - Start Date for publishing 
    • end date - End Date for publishing
    • schedule (boolean value) - Is publishing scheduled?

  • Display the results.
The code snippet, given below, shows the logic.
  1. // Input Parameters      
  2. string siteUrl = "https://nakkeerann.sharepoint.com/";  
  3. string userName = "[email protected]";  
  4. string password = "***";  
  5.   
  6. // PnP component to set context      
  7. AuthenticationManager authManager = new AuthenticationManager();  
  8. try  
  9. {  
  10.     // Get and set the client context  
  11.     // Connects to SharePoint online site using inputs provided  
  12.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  13.     {  
  14.         // Input Parameters  
  15.         string pageName = "TestPage";  
  16.         string pageTemplate = "BlankWebPartPage";  
  17.         string pageTitle = "Test Page";  
  18.         bool toBePublished = true;  
  19.         DateTime startTime = DateTime.Now;  
  20.         DateTime endTime = DateTime.Now;  
  21.         bool scheduled = true;  
  22.         // Adds Publishing Page  
  23.         clientContext.Site.RootWeb.AddPublishingPage(pageName, pageTemplate, pageTitle, toBePublished, null,startTime,endTime,scheduled);  
  24.   
  25.         // Output  
  26.         Console.WriteLine("Publishing Page Scheduled for creation/Created");  
  27.         Console.ReadKey();  
  28.     }  
  29. }  
  30. catch (Exception ex)  
  31. {  
  32.     Console.WriteLine("Error Message: " + ex.Message);  
  33.     Console.ReadKey();  
  34. }  
The snapshot, given below, shows the created page.



Summary

Thus, you have learned adding a publishing page to SharePoint online pages library, using PnP Core CSOM library components. To implement the same on 'On Premise sites', Authentication Method needs to be changed.