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.

  1. Page Name

  2. Template Name

  3. Page Title

  • Other optional parameters are as follows.

  1. publish (boolean value) - To be published?

  2. folder - Target folder name. If left as blank or null, created under pages library 

  3. start date - Start Date for publishing 

  4. end date - End Date for publishing

  5. 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 = "nav@nakkeerann.onmicrosoft.com";  

  4. string password = "***";  

  5. // PnP component to set context    

  6. AuthenticationManager authManager = new AuthenticationManager();  

  7. try

  8. {  

  9. // Get and set the client context

  10. // Connects to SharePoint online site using inputs provided

  11. using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))      

  12. {  

  13. // Input Parameters

  14. string pageName = "TestPage";  

  15. string pageTemplate = "BlankWebPartPage";  

  16. string pageTitle = "Test Page";  

  17. bool toBePublished = true;          

  18. DateTime startTime = DateTime. Now;          

  19. DateTime endTime = DateTime. Now;  

  20. bool scheduled = true;  

  21. // Adds Publishing Page         

  22. clientContext.Site.RootWeb.AddPublishingPage(pageName, pageTemplate, pageTitle, toBePublished, 

  23. null, startTime, endTime, scheduled);  

  24. // Output         

  25. Console.WriteLine("Publishing Page Scheduled for creation/Created");         

  26.  Console.ReadKey();      

  27. }  

  28. }  

  29. catch (Exception ex)  

  30. {      

  31. Console.WriteLine("Error Message: " + ex.Message);      

  32. Console.ReadKey();  

  33. }  

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. 

Recent Posts

See All