Creating Wiki Page On SharePoint Online Using PnP Core CSOM Library

Introduction
 
In this article, you will learn how to create a Wiki page on SharePoint Online site with various methods, using PnP Core CSOM library.
 
The main advantage of using PnP Core libraries is the reduced code to get/set the required information. The required object can be retrieved/created/updated 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 
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)
Create Wiki page
 
In this approach, you will learn about the creation of a Wiki page on a Wiki page library.
 
The following steps explain the process in detail.
  • Input the site detail, user details for authentication, and page details.
  • Authenticate and get the client context of the site and then the necessary Web object.
  • Using Web object, add the wiki page to the wiki pages library using AddWikiPage method. The required parameters are given below.

    • Page library name
    • Page name

  • Display the results.
The following code snippet shows you the way to create a wiki page. 
  1. // Input Parameters      
  2. string siteUrl = "https://abc.sharepoint.com/";  
  3. string userName = "[email protected]";  
  4. string password = "***";  
  5.   
  6. AuthenticationManager authManager = new AuthenticationManager();  
  7. try  
  8. {  
  9.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  10.     {  
  11.   
  12.         // Input Parameters  
  13.         string libraryName = "WikiPages";  
  14.         string pageName = "TestPage.aspx";  
  15.   
  16.         // Creating Wiki Page  
  17.         string wikiPage = clientContext.Site.RootWeb.AddWikiPage(libraryName, pageName);  
  18.   
  19.         Console.WriteLine("Created Wiki Page Url : " + wikiPage);  
  20.         Console.ReadKey();  
  21.   
  22.     }  
  23. }  
  24. catch (Exception ex)  
  25. {  
  26.     Console.WriteLine("Error Message: " + ex.Message);  
  27.     Console.ReadKey();  
  28. }   
Create Wiki Page with Page Content
 
In this approach, you will create the Wiki page with page content on Wiki page library.
 
The following steps explain the process in detail.
  • Input the site detail, user details for authentication, and page details.
  • Authenticate and get the client context of the site and then the necessary Web object.
  • Using Web object, add the Wiki page to the wiki pages library with the content as input, using AddWikiPageByUrl method. The required parameters are given below.

    • Page URL
    • Page content

  • Display the results.
The following code snippet shows the way to create a Wiki page with page content.
  1. // Input Parameters      
  2. string siteUrl = "https://abc.sharepoint.com/";  
  3. string userName = "[email protected]";  
  4. string password = "***";  
  5.   
  6. AuthenticationManager authManager = new AuthenticationManager();  
  7. try  
  8. {  
  9.     using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))  
  10.     {  
  11.   
  12.         // Input Parameters  
  13.         string pageContent = "<div>Test page for PnP Core components learning</div>";  
  14.         string pageUrl = "/WikiPages/TestPage2.aspx";  
  15.   
  16.         clientContext.Site.RootWeb.AddWikiPageByUrl(pageUrl, pageContent);  
  17.                       
  18.         Console.WriteLine("Wiki Page Created");  
  19.         Console.ReadKey();  
  20.   
  21.     }  
  22. }  
  23. catch (Exception ex)  
  24. {  
  25.     Console.WriteLine("Error Message: " + ex.Message);  
  26.     Console.ReadKey();  
  27. }  
Summary

Thus, you have learned how to create Wiki pages by multiple approaches, using PnP Core CSOM library programming. 

Next Recommended Readings