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.

  1. Page library name

  2. 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 = "abc@abc.onmicrosoft.com";  

  4. string password = "***";  

  5. AuthenticationManager authManager = new AuthenticationManager();  

  6. try

  7. {  

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

  9. {  

  10. // Input Parameters

  11. string libraryName = "WikiPages";  

  12. string pageName = "TestPage.aspx";  

  13. // Creating Wiki Page

  14. string wikiPage = clientContext.Site.RootWeb.AddWikiPage(libraryName, pageName);           

  15. Console.WriteLine("Created Wiki Page Url : " + wikiPage);          

  16. Console.ReadKey();       

  17. }  

  18. }  

  19. catch (Exception ex)  

  20. {      

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

  22. Console.ReadKey();  

  23. }

  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.

  1. Page URL

  2. 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 = "abc@abc.onmicrosoft.com";  

  4. string password = "***";  

  5. AuthenticationManager authManager = new AuthenticationManager();  

  6. try

  7. {  

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

  9. {  

  10. // Input Parameters

  11. string pageContent = "<div>Test page for PnP Core components learning</div>";  

  12. string pageUrl = "/WikiPages/TestPage2.aspx";           

  13. clientContext.Site.RootWeb.AddWikiPageByUrl(pageUrl, pageContent);           

  14. Console.WriteLine("Wiki Page Created");          

  15. Console.ReadKey();       

  16. }  

  17. }  

  18. catch (Exception ex)  

  19. {      

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

  21. Console.ReadKey();  

  22. }

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

Recent Posts

See All