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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
- using System.Security;
- using Microsoft.SharePoint.Client.Publishing;
-
- namespace CreatepublishingPage
- {
- class Program
- {
- static void Main(string[] args)
- {
- AddPublishingPage();
- }
- public void AddPublishingPage()
- {
- ClientContext context;
- string pageName="CustomPage";
- Web webSite = context.Web;
- context.Load(webSite);
- PublishingWeb web = PublishingWeb.GetPublishingWeb(context, webSite);
- context.Load(web);
-
- if (web != null)
- {
- List pages = context.Site.RootWeb.Lists.GetByTitle(“Pages”);
- ListItemCollection defaultPages = pages.GetItems(CamlQuery.CreateAllItemsQuery());
- context.Load(defaultPages , items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName == pageName));
- context.ExecuteQuery();
- if (defaultPages != null && defaultPages .Count > 0)
- {
- }
- else
- {
-
- List publishingLayouts = context.Site.RootWeb.Lists.GetByTitle(“Master Page Gallery”);
- ListItemCollection allItems = publishingLayouts.GetItems(CamlQuery.CreateAllItemsQuery());
- context.Load(allItems, items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName ==“BlankWebPartPage”));
- context.ExecuteQuery();
- ListItem layout = allItems.Where(x => x.DisplayName == “BlankWebPartPage”).FirstOrDefault();
- context.Load(layout);
- PublishingPageInformation publishingPageInfo = newPublishingPageInformation();
- publishingPageInfo.Name = pageName;
- publishingPageInfo.PageLayoutListItem = layout;
- PublishingPage publishingPage = web.AddPublishingPage(publishingPageInfo);
- publishingPage.ListItem.File.CheckIn(string.Empty, CheckinType.MajorCheckIn);
- publishingPage.ListItem.File.Publish(string.Empty);
- publishingPage.ListItem.File.Approve(string.Empty);
- context.Load(publishingPage);
- context.Load(publishingPage.ListItem.File, obj => obj.ServerRelativeUrl);
- context.ExecuteQuery();
- }
- }
- }
-
-
-
- }
-
- }
-
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.