How to Create WikiPages Programmatically in Community Site Templates in SharePoint Office 365 using C# Client Side Object Model (CSOM)


Sathish Nadarajan
SharePoint MVP
Published On :   05 Sep 2016
Visit Count
Today :  3    Total :   5072
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


In this article, let us see how to create a Wiki Page Programmatically in a Community Site Template in SharePoint Online using C#.

To Achieve this, initially I was thinking like, we need to search for the Page Layout and create the page using the Layout identified. But the usage of PNP made our life very simple and straight forward.

Please find the Snippet, which is very straight forward and does not require any explanation.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
 using Microsoft.SharePoint.Client;
 using System.Security;
 using Microsoft.Online.SharePoint.TenantAdministration;
 using OfficeDevPnP.Core;
 
 namespace Console.Office365
 {
     class Program
     {
         static void Main(string[] args)
         {
             CreateWikiHomePage();
         }
 
 
         public static void CreateWikiHomePage()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://*****.sharepoint.com/sites/CommunitySite/";
             string userName = "sathish@*********.onmicrosoft.com";
             string password = "********";
 
             using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = ctx.Web;
                 ctx.Load(web);
                 ctx.ExecuteQueryRetry();
 
                 List sitePagesList = web.Lists.GetByTitle("Site Pages");
 
                 ctx.Load(sitePagesList);
                 ctx.Load(sitePagesList.RootFolder);
                 ctx.ExecuteQueryRetry();
 
                 var pageTitle = "WikiPage1.aspx";
                 sitePagesList.ParentWeb.AddWikiPage(sitePagesList.Title, pageTitle);
                 //Add a Layout to the Wiki Page.  There are some default layouts given by PNP itself.
                 ctx.Web.AddLayoutToWikiPage(WikiPageLayout.OneColumnSideBar, sitePagesList.RootFolder.ServerRelativeUrl + "/" + pageTitle);
                 ctx.ExecuteQueryRetry();
             }
         }
     }
     
 }
 

Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

SharePoint Analytics