I have deployed a custom site definition.. Features available in onet.xml are -

Site Features-

SharePoint Server Publishing Infrastructure

Publishing Workflow feature

Web Features

Office SharePoint Server Publishing.

I also have 3 modules, that deploys master pages, page layouts and display templates respectively.

Now I have added a feature receiver and on feature activating , I am creating Publishing Pages based on the deployed custom page layout. Code is below.

 public override void FeatureActivated(SPFeatureReceiverProperties properties)
            System.IO.File.AppendAllText(@"C:\Users\ADLATempSPAdmin\Desktop\Logging.txt", "started");
            SPSite site1 = properties.Feature.Parent as SPSite;
            SPWeb web1 = site1.OpenWeb();
            PublishingSite PublishingSite = new PublishingSite(site1);
            PublishingWeb publishingWeb1 = PublishingWeb.GetPublishingWeb(web1);
            string pageName1 = "MyCustomPage.aspx";
            SPContentType ctype = PublishingSite.ContentTypes["Article Page"];
            PageLayoutCollection pageLayouts = PublishingSite.GetPageLayouts(ctype, true);
            System.IO.File.AppendAllText(@"C:\Users\ADLATempSPAdmin\Desktop\Logging.txt", web1.Url);
            PageLayout pageLayout = pageLayouts[web1.Url + "/_catalogs/masterpage/SchoolLayout.aspx"];

            //PageLayout[] pageLayouts1 = publishingWeb1.GetAvailablePageLayouts();
            //PageLayout currPageLayout = pageLayouts1[0];
            PublishingPageCollection pages = publishingWeb1.GetPublishingPages();
            PublishingPage newPage1 = pages.Add(pageName1, pageLayout);
            newPage1.ListItem[FieldId.PublishingPageContent] = "This is my content";
            newPage1.CheckIn("This is just a comment");


But I am getting error while creating a site collection, based on my custom site definition. "

The site is not valid. The ‘Pages’ document library is missing.

I read that, our feature receivers should not have dependencies on the features written in onet.xml. Is that right?

If so, my publishing feature is activated after my event is fired. so "Pages" document library is not present.

How should I proceed now. I am scratching my head for days in this. Please help.

  • Did you get it working? Did my code helps you anyhow? – Hardik Jun 16 '16 at 17:31

Below is the code to Create a Publishing Page Programmatically in SharePoint 2013. You will get the idea from it.

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;

                 System.Text.StringBuilder sb = new System.Text.StringBuilder();
                 sb.Append(@"<script language='javascript'>");
                 sb.Append(@"CallBackSuccess('" + strCreatedPageURL + "');");
                 Page.ClientScript.RegisterStartupScript(this.GetType(), "JCall1", sb.ToString(), false);
             catch (Exception ex)
                 System.Text.StringBuilder sb = new System.Text.StringBuilder();
                 sb.Append(@"<script language='javascript'>");
                 sb.Append(@"CallBackFailure('" + ex.Message + "');");
                 Page.ClientScript.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;
  • Thanks for the reply Hardik. But In my feature receiver, I am not able to use ClientScriptManager script = Page.ClientScript; Is there something, I am missing? In a normal console app, I am able to create a page, based on my above code. But in a feature receiver , I am not able to do so. – CHANDRU D Jun 17 '16 at 10:23
  • Please use Page.ClientScript.. I have also updated my code so kindly try it. – Hardik Jun 17 '16 at 12:27
  • Have you tried it? Did you get it working? – Hardik Jul 5 '16 at 9:30
  • Hardik. My colleague has finished that part. He had the working code already. Thanks – CHANDRU D Jul 11 '16 at 5:53
  • Glad to hear that. Did my code helped you anyhow? As it is doing the same as per your requirement. – Hardik Jul 11 '16 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.