This blog post explains how to create/add publishing pages to a Publishing Site in SharePoint 2013 using the managed Client Object Model (CSOM) .
Microsoft.SharePoint.Client.Publishing DLL to achieve this.
Code Sample:
- 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 so, please let us know at the bottom of this page. If not, let us know what was confusing or missing I’ll use your feedback to double-check the facts, add info, and update this article.
Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
ReplyDeleteSharepoint Training in Chennai