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. namespace CreatepublishingPage
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. AddPublishingPage();
  16. }
  17. public void AddPublishingPage()
  18. {
  19. ClientContext context;
  20. string pageName="CustomPage";
  21. Web webSite = context.Web;
  22. context.Load(webSite);
  23. PublishingWeb web = PublishingWeb.GetPublishingWeb(context, webSite);
  24. context.Load(web);
  25. if (web != null)
  26. {
  27. List pages = context.Site.RootWeb.Lists.GetByTitle(“Pages”);
  28. ListItemCollection defaultPages = pages.GetItems(CamlQuery.CreateAllItemsQuery());
  29. context.Load(defaultPages , items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName == pageName));
  30. context.ExecuteQuery();
  31. if (defaultPages != null && defaultPages .Count > 0)
  32. {
  33. }
  34. else
  35. {
  36. List publishingLayouts = context.Site.RootWeb.Lists.GetByTitle(“Master Page Gallery”);
  37. ListItemCollection allItems = publishingLayouts.GetItems(CamlQuery.CreateAllItemsQuery());
  38. context.Load(allItems, items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName ==“BlankWebPartPage”));
  39. context.ExecuteQuery();
  40. ListItem layout = allItems.Where(x => x.DisplayName == “BlankWebPartPage”).FirstOrDefault();
  41. context.Load(layout);
  42. PublishingPageInformation publishingPageInfo = newPublishingPageInformation();
  43. publishingPageInfo.Name = pageName;
  44. publishingPageInfo.PageLayoutListItem = layout;
  45. PublishingPage publishingPage = web.AddPublishingPage(publishingPageInfo);
  46. publishingPage.ListItem.File.CheckIn(string.Empty, CheckinType.MajorCheckIn);
  47. publishingPage.ListItem.File.Publish(string.Empty);
  48. publishingPage.ListItem.File.Approve(string.Empty);
  49. context.Load(publishingPage);
  50. context.Load(publishingPage.ListItem.File, obj => obj.ServerRelativeUrl);
  51. context.ExecuteQuery();
  52. }
  53. }
  54. }
  55. }
  56. }

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.