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.
-
- string siteUrl = "https://abc.sharepoint.com/";
- string userName = "[email protected]";
- string password = "***";
-
- AuthenticationManager authManager = new AuthenticationManager();
- try
- {
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
-
- string libraryName = "WikiPages";
- string pageName = "TestPage.aspx";
-
-
- string wikiPage = clientContext.Site.RootWeb.AddWikiPage(libraryName, pageName);
-
- Console.WriteLine("Created Wiki Page Url : " + wikiPage);
- Console.ReadKey();
-
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
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.
- Display the results.
The following code snippet shows the way to create a Wiki page with page content.
-
- string siteUrl = "https://abc.sharepoint.com/";
- string userName = "[email protected]";
- string password = "***";
-
- AuthenticationManager authManager = new AuthenticationManager();
- try
- {
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
-
- string pageContent = "<div>Test page for PnP Core components learning</div>";
- string pageUrl = "/WikiPages/TestPage2.aspx";
-
- clientContext.Site.RootWeb.AddWikiPageByUrl(pageUrl, pageContent);
-
- Console.WriteLine("Wiki Page Created");
- Console.ReadKey();
-
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
Summary
Thus, you have learned how to create Wiki pages by multiple approaches, using PnP Core CSOM library programming.