Advertisement

Responsive Advertisement

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 the managed Client Object Model (CSOM) .
Microsoft.SharePoint.Client.Publishing DLL to achieve this.
The Microsoft.SharePoint.Client.Publishing namespace will be used in the below sample.
Code 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.                      
  26.                     if (web != null)
  27.                     {
  28.                     List pages = context.Site.RootWeb.Lists.GetByTitle(“Pages”);
  29.                     ListItemCollection defaultPages = pages.GetItems(CamlQuery.CreateAllItemsQuery());
  30.                     context.Load(defaultPages , items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName == pageName));
  31.                     context.ExecuteQuery();
  32.                     if (defaultPages  != null && defaultPages .Count > 0)
  33.                     {
  34.                     }
  35.                     else
  36.                     {
  37.                     List publishingLayouts = context.Site.RootWeb.Lists.GetByTitle(“Master Page Gallery”);
  38.                     ListItemCollection allItems = publishingLayouts.GetItems(CamlQuery.CreateAllItemsQuery());
  39.                     context.Load(allItems, items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName ==“BlankWebPartPage”));
  40.                     context.ExecuteQuery();
  41.                     ListItem layout = allItems.Where(x => x.DisplayName == “BlankWebPartPage”).FirstOrDefault();
  42.                     context.Load(layout);
  43.                     PublishingPageInformation publishingPageInfo = newPublishingPageInformation();
  44.                     publishingPageInfo.Name = pageName;
  45.                     publishingPageInfo.PageLayoutListItem = layout;
  46.                     PublishingPage publishingPage = web.AddPublishingPage(publishingPageInfo);
  47.                     publishingPage.ListItem.File.CheckIn(string.Empty, CheckinType.MajorCheckIn);
  48.                     publishingPage.ListItem.File.Publish(string.Empty);
  49.                     publishingPage.ListItem.File.Approve(string.Empty);
  50.                     context.Load(publishingPage);
  51.                     context.Load(publishingPage.ListItem.File, obj => obj.ServerRelativeUrl);
  52.                     context.ExecuteQuery();
  53.                     }
  54.                     }
  55.                     }
  56.          
  57.      }
  58.      
  59.  }


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.



Post a Comment

1 Comments

  1. 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.
    Sharepoint Training in Chennai

    ReplyDelete