How to Create a Modern Pages Programmatically in SharePoint Office 365 using Patterns and Practice (OfficeDevPNP)

Sathish Nadarajan
 
Solution Architect
May 2, 2018
 
Rate this article
 
Views
7126

In the earlier article, we saw how to create the modern site programmatically. As a follow up, we need to create the pages and add the desired webparts based on the requirement.

The below piece of code, will create the pages and add the default webparts on it.

 static void Main(string[] args)
         {
             string siteUrl = "https://*****.sharepoint.com/Sites/DemoCommunicationSite";
             string userName = "sathish@****.onmicrosoft.com";
             string password = "***";
 
 
 
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             #region O365
             using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = ctx.Web;
                 ctx.Load(web.Lists);
                 ctx.Load(web, w => w.SupportedUILanguageIds);
                 ctx.Load(web);
 
                 ctx.ExecuteQueryRetry();
 //Create the Page
 var page2 = ctx.Web.AddClientSidePage("PageWithSections.aspx", true);
                 page2.AddSection(CanvasSectionTemplate.ThreeColumn, 5);
                 //page2.AddSection(CanvasSectionTemplate.TwoColumn, 10);
                 page2.Save();
 
 // Initialize the WEbpart
                 ClientSideWebPart videoEmbedWp = page2.InstantiateDefaultWebPart(DefaultClientSideWebParts.VideoEmbed);
 
 //The below are the sample video webpart properties which I have given.  We can add any OOB webpart like this.
                 videoEmbedWp.Properties["videoSource"] = "https://sppalsmvp.sharepoint.com/portals/hub/_layouts/15/PointPublishing.aspx?app=video&p=p&chid=d54ed63d-6577-4aa8-ab90-b07215dfa55b&vid=b60cb3f6-7873-4caa-a83b-ec035c472e74";
                 videoEmbedWp.Properties["captionText"] = "My video";
                 videoEmbedWp.Properties["showInfo"] = false;
                 videoEmbedWp.Properties["embedCode"] = "<iframe width=853 height=480 src='https://sppalsmvp.sharepoint.com/portals/hub/_layouts/15/PointPublishing.aspx?app=video&p=p&chid=d54ed63d-6577-4aa8-ab90-b07215dfa55b&vid=b60cb3f6-7873-4caa-a83b-ec035c472e74&width=853&height=480&autoPlay=false&showInfo=true' allowfullscreen></iframe>";
                 videoEmbedWp.Title = "Associated video";
 
 //Get the section
                 CanvasSection section = page2.Sections[1];
 //Add the webpart to the section
                 page2.AddControl(videoEmbedWp, section.Columns[1], 0);
                 page2.Save();
 }
 }
 

Happy Coding,

Sathish Nadarajan.

Category : Programmatically

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment