SharePoint Pals
 | Sign In
How to Create a Publishing Page Programmatically in SharePoint 2013
by Sathish Nadarajan 30 Mar 2015
SharePoint MVP
Today  :   19     Total  :    12299

In one of the Older Article, we saw how to create a publishing page using PowerShell. In the same manner, in this article let us see, how to create a publishing page using C#.

The actual requirement is, we need to develop an application Page and open it as a popup. From that popup, user can enter the title and url of the publishing page and clicks on the Create Page Button. We need to take values from the screen and based on the content type selected, we need to get the corresponding Page Layout and using that layout create the page.

The Input screen was something looks like


The Code Behind is like,

 protected void btnCreatePage_OnClick(object sender, EventArgs e)
                 string currentSite = SPContext.Current.Site.OpenWeb().Title;
                 string strPageURL = txtURL.Text.Trim() + ".aspx";
                 string strPageTitle = txtTitle.Text.Trim();
                 string strCreatedPageURL = string.Empty;
                 string layoutName = GetLayoutName(ddlContentType.SelectedValue); 
                     using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
                         Microsoft.SharePoint.Publishing.PublishingSite oPublishingSite = new Microsoft.SharePoint.Publishing.PublishingSite(oSite);
                         SPWeb web = SPContext.Current.Site.OpenWeb();
                         web.AllowUnsafeUpdates = true;
                         Guid PagesID = web.Lists["Pages"].ID;
                         web.AllProperties["__PagesListId"] = PagesID.ToString();
                         //web.AllProperties["__PublishingFeatureActivated"] = "True";
                         Microsoft.SharePoint.Publishing.PublishingWeb oPublishingWeb = Microsoft.SharePoint.Publishing.PublishingWeb.GetPublishingWeb(web);
                         Microsoft.SharePoint.Publishing.PageLayoutCollection layoutCollection = oPublishingSite.GetPageLayouts(false);
                         Microsoft.SharePoint.Publishing.PageLayout layout = layoutCollection[layoutName];
                         Microsoft.SharePoint.Publishing.PublishingPage page = oPublishingWeb.GetPublishingPage(web.Url + "/Pages/" + strPageURL);
                         if (page == null)
                             page = oPublishingWeb.AddPublishingPage(strPageURL, layout);
                             throw new Exception("A page with the given URL already exists. Please try a different URL");
                         if (page.ListItem.File.CheckedOutByUser == null)
                         page.Title = strPageTitle;
                         page.CheckIn("Page Created");
                         //page.ListItem.File.Publish("Page Published");
                         web.AllowUnsafeUpdates = false;
                         strCreatedPageURL = web.Url + "/Pages/" + strPageURL;
                 ClientScriptManager script = Page.ClientScript;
                 System.Text.StringBuilder sb = new System.Text.StringBuilder();
                 sb.Append(@"<script language='javascript'>");
                 sb.Append(@"CallBackSuccess('" + strCreatedPageURL + "');");
                 script.RegisterStartupScript(this.GetType(), "JCall1", sb.ToString(), false);
             catch (Exception ex)
                 ClientScriptManager script = Page.ClientScript;
                 System.Text.StringBuilder sb = new System.Text.StringBuilder();
                 sb.Append(@"<script language='javascript'>");
                 sb.Append(@"CallBackFailure('" + ex.Message + "');");
                 script.RegisterStartupScript(this.GetType(), "JCall1", sb.ToString(), false);
                 Logger.LogException("Error has occured on CreatePage Functionality and error message :: " + ex.Message, ex.StackTrace, "CreatePage", "btnCreatePage_OnClick", null);
         private string GetLayoutName(string contentTypeName)
             string layoutURL = string.Empty;
             switch (contentTypeName)
                 case "Article":
                     layoutURL = "/_catalogs/masterpage/CustomArticle.aspx";
                 case "Convention":
                     layoutURL = "/_catalogs/masterpage/CustomConvention.aspx";
                     layoutURL = "/_catalogs/masterpage/BlankWebPartPage.aspx";
             return layoutURL;

The code was straight forward and we are calling a CallBackSuccess Method from the javascript.

This method is used to Open the page in Edit Mode. A simple piece of Javascritp is as follows.

 function OpenInEditMode(returnValue) {
     var temp = '?ControlMode=Edit' + '&amp;DisplayMode=Design'
     window.location.href = returnValue + temp;

The extra Query strings which we need to pass are ControlMode and DisplayMode. This will open the newly created page in Edit Mode.

Happy Coding.

Sathish Nadarajan.

blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter October 23, 22:21
How to Add/Remove User Custom Actions (in Site Actions Menu) Programmatically using CSOM PNP in SharePoint -

Twitter October 21, 21:34
How to Add a JS Link Reference to the Display Form or Any other ASPX Programmatically using CSOM PNP in SharePoint -

Twitter October 20, 13:01
How to Add a JS Link Reference to the NewForm and EditForm Programmatically using CSOM PNP in SharePoint Office 365-

Twitter October 12, 12:15
How to Deploy Provider Hosted Apps (Add-Ins) by App Stapling in SharePoint Office 365 -

Twitter October 11, 13:39
How to Deploy Provider HostedApp programmatically using CSOM in SharePoint Office 365 Activating Developer Feature -

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation