How to Create WebPart Page 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 :  1    Total :   5681
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


In the earlier article, we saw how to create the Wikipage. In the same manner, if we look at the Site, there are one more Page, we can create in a Community Site Template. That is nothing but a WebPart Page.

clip_image002

Let us see how to create a WebPart Page Programmatically in a Community Site Template in SharePoint Online using C#.

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)
         {
             CreateWebPartPage();
         }
 
 
         public static void CreateWebPartPage()
         {
             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 = "webpartpage.aspx";
                 sitePagesList.RootFolder.Files.AddTemplateFile(sitePagesList.RootFolder.ServerRelativeUrl + "/" + pageTitle, TemplateFileType.StandardPage);
                 
                 ctx.ExecuteQueryRetry();
             }
         }
     }
     
 }
 
 

After Executing the Code, the page will look like below.

clip_image004

Happy Coding,

Sathish Nadarajan.

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

KWizCom Scan