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("[email protected]", securstr);
- List Library = ctx.Web.Lists.GetByTitle("site pages");
- ctx.Load(Library);
- ctx.ExecuteQuery();
-
- ListItem oItem = Library.RootFolder.Files.AddTemplateFile("/sites/NewSite/SitePages/MyModernPage.aspx", TemplateFileType.ClientSidePage).ListItemAllFields;
-
- 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 it's created, we can check it in the sitepage Library.