How to create modern site page using CSOM

Introduction:-

In a SharePoint modern page, we can access various SharePoint sites easily within an organization. We can also find the news from the site, frequent site which is visited and other suggested news posts as well as the events. It provides a fast and easy way to develop a modern responsive page using modern webparts.

For creating a modern page first, we create the list item object in the site page library and assign the correct content type with some other additional properties to list item object.

Follow the code shown below:

Step 1:- Set the credential for SiteUrl.

Step 2:- Get the Library name.

Step 3:- Add the template file in list item by giving the ServerRelativeUrl having modern page name (take modern page name as per your requirement. Here we have used “MyModernPage”).

Step 4:- Add the correct content type and properties for the modern page.

   class Program
;      {
        static void Main(string[] args)
        {
            using (ClientContext ctx = new ClientContext("https://contoso.SharePoint.com/sites/NewSite"))
            {
                string passWord = "PassWord";
                SecureString securstr = new SecureString();
                foreach (char ch in passWord)
                {
                    securstr.AppendChar(ch);
                }
                ctx.Credentials = new SharePointOnlineCredentials("username@contoso.onmicrosoft.com", securstr);
                List Library = ctx.Web.Lists.GetByTitle("site pages");
                ctx.Load(Library);
                ctx.ExecuteQuery();
                // pagesLibrary is List object for the "site pages" library of the site
                ListItem oItem = 
Library.RootFolder.Files.AddTemplateFile("/sites/NewSite/SitePages/MyModernPage.aspx", TemplateFileType.ClientSidePage).ListItemAllFields;

               // Make this page a "modern" page
               oItem ["ContentTypeId"] = "0x0101009D1CB255DA76424F860D91F20E6C4118";
               oItem ["Title"] = System.IO.Path.GetFileNameWithoutExtension("MyModernpage.aspx");
               oItem ["ClientSideApplicationId"] = "b6917cb1-93a0-4b97-a84d-7cf49975d4ec";
               oItem ["PageLayoutType"] = "Article";
               oItem ["PromotedState"] = "0";
               oItem ["CanvasContent1"] = "<div></div>";
               oItem ["BannerImageUrl"] = "/_layouts/15/images/sitepagethumbnail.png";
               oItem .Update();
               ctx.Load(oItem );
               ctx.ExecuteQuery();
               Console.WriteLine("successfully created mordern page in library");
               Console.ReadLine();
        }
    }
}  

Step 5:- Now its created, we can check it in the sitepage Library.

Keywords:

  1. How to create modern page in SharePoint using CSOM.
  2. Programmatically create a modern site page using client server object model.
  3. Create SharePoint modern page using CSOM.
  4. Using C# create a modern site page in SharePoint online.
  5. Creating modern page in sitepage library using CSOM.

Leave a Reply

Your email address will not be published. Required fields are marked *