Create A Publishing Page In SharePoint 2013 Using CSOM

This blog post explains how to create/add publishing pages to a Publishing Site in SharePoint 2013, using managed Client Object Model (CSOM) .

Microsoft.SharePoint.Client.Publishing DLL can achieve this.

The Microsoft.SharePoint.Client.Publishing namespace will be used in the below sample.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;    
  6. using Microsoft.SharePoint.Client;  
  7. using System.Security;  
  8. using Microsoft.SharePoint.Client.Publishing;  
  9.    
  10. namespace CreatepublishingPage  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             AddPublishingPage();  
  17.         }  
  18. public void AddPublishingPage()  
  19.                 {  
  20.                 ClientContext context;  
  21.                 string pageName="CustomPage";  
  22.                 Web webSite = context.Web;  
  23.                 context.Load(webSite);  
  24.                 PublishingWeb web = PublishingWeb.GetPublishingWeb(context, webSite);  
  25.                 context.Load(web);  
  26.                    
  27.                 if (web != null)  
  28.                 {  
  29.                 List pages = context.Site.RootWeb.Lists.GetByTitle(“Pages”);  
  30.                 ListItemCollection defaultPages = pages.GetItems(CamlQuery.CreateAllItemsQuery());  
  31.                 context.Load(defaultPages , items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName == pageName));  
  32.                 context.ExecuteQuery();  
  33.                 if (defaultPages  != null && defaultPages .Count > 0)  
  34.                 {  
  35.                 }  
  36.                 else  
  37.                 {  
  38.   
  39.                 List publishingLayouts = context.Site.RootWeb.Lists.GetByTitle(“Master Page Gallery”);  
  40.                 ListItemCollection allItems = publishingLayouts.GetItems(CamlQuery.CreateAllItemsQuery());  
  41.                 context.Load(allItems, items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName ==“BlankWebPartPage”));  
  42.                 context.ExecuteQuery();  
  43.                 ListItem layout = allItems.Where(x => x.DisplayName == “BlankWebPartPage”).FirstOrDefault();  
  44.                 context.Load(layout);  
  45.                 PublishingPageInformation publishingPageInfo = newPublishingPageInformation();  
  46.                 publishingPageInfo.Name = pageName;  
  47.                 publishingPageInfo.PageLayoutListItem = layout;  
  48.                 PublishingPage publishingPage = web.AddPublishingPage(publishingPageInfo);  
  49.                 publishingPage.ListItem.File.CheckIn(string.Empty, CheckinType.MajorCheckIn);  
  50.                 publishingPage.ListItem.File.Publish(string.Empty);  
  51.                 publishingPage.ListItem.File.Approve(string.Empty);  
  52.                 context.Load(publishingPage);  
  53.                 context.Load(publishingPage.ListItem.File, obj => obj.ServerRelativeUrl);  
  54.                 context.ExecuteQuery();  
  55.                 }  
  56.                 }  
  57.                 }  
  58.   
  59.    
  60.           
  61.     }  
  62.        
  63. }  
  64.    

Conclusion

Was my Blog helpful? If yes, please let me know; and if not, please explain what was confusing or missing. I’ll use your feedback to double-check the facts, add info, and update this article.

Ebook Download
View all
Learn
View all