How to add a Publishing Page in SharePoint 2013 using Client Object Model
This blog post explains how to create/add publishing pages to a Publishing Site in SharePoint 2013 using the managed Client Object Model (CSOM) . We will be using the Microsoft.SharePoint.Client.Publishing DLL to achieve this. The Microsoft.SharePoint.Client.Publishing namespace will be used in the below sample.
Code Sample:
The below method takes the ClientContext object and the name of the page to be created as parameters. It gets the reference of publishing web from the current web and creates a publishing page in the web.
public void AddPublishingPage(ClientContext context, string pageName)
{
// Get current web
Web webSite = context.Web;
context.Load(webSite);
PublishingWeb web = PublishingWeb.GetPublishingWeb(context, webSite);
context.Load(web);
if (web != null)
{
// Get Pages Library
List pages = context.Site.RootWeb.Lists.GetByTitle("Pages");
ListItemCollection existingPages = pages.GetItems(CamlQuery.CreateAllItemsQuery());
context.Load(existingPages, items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName == pageName));
context.ExecuteQuery();
// Check if page already exists
if (existingPages != null && existingPages.Count > 0)
{
// Page already exists
}
else
{
// Get Publishing Page Layouts
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);
// Create a publishing page
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();
}
}
}
Comments
Anonymous
June 19, 2013
Hi Prasanna, Could you please explain the method you used to create the page One small doubt if i use the code you provided the page is getting created in PAGES library there is a difference in URL's signature for the page i create directly and using code ex:"http://deheremap4840:1234/sites/brand_adidas/Pages/New.aspx" is the one i created using code and "http://deheremap4840:1234/sites/brand_adidas/test" the one i created directly in share point why i need to mention 'pages/New.aspx' for NEW page and no need for TEST.aspx eventhough both are in same PAGES folder, Thanks in advance.Anonymous
October 06, 2013
Thanks a lot for sharing the articles.Anonymous
October 27, 2013
Hi, you code is so dirty!!!! First of all, the best method is to use SP query to select needed page and page's Layout. It reduces performance issue. Secondary, context.Web and context.Site.RootWeb can be different webs. Each these webs can contain Pages library.Anonymous
November 23, 2013
The comment has been removedAnonymous
April 07, 2015
You can Deploy and Change Master page in CSOM, a nice post similar to the above : www.sharepointsol.com/.../apply-master-page-using-csom.htmlAnonymous
November 29, 2015
I love this post. but i found more useful link for you. http://pakistanitips.com/