Question

After provisioning a site the new site does not have the right page layout like I instructed the code to be. I went to site settings of the new site and clicked on page layout and site template. And sure enough it was inheriting from parent instead of using what I told it to use. hmm. Can anyone see any fault in the code? The page layout pages shows in the list.

private static void AddPage(SPSite site, SPWeb web, string pageName, string strPageTitle)
{
  PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
  PageLayout[] pageLayouts = publishingWeb.GetAvailablePageLayouts();
  PageLayout currPageLayout = null;
  foreach (PageLayout pl in pageLayouts)
    {
     if (pl.Name.Equals("RMLayout.aspx", StringComparison.InvariantCultureIgnoreCase))
          {
            currPageLayout = pl;
            break;
          }
    }
   PublishingPageCollection pages = publishingWeb.GetPublishingPages();
   PublishingPage newPage = pages.Add(pageName, currPageLayout);
   publishingWeb.Update();
   string content = "";  // "Welcome to <strong>My Page</strong>";
   newPage.ListItem[FieldId.PublishingPageContent] = content;
   newPage.Title = strPageTitle;

    newPage.ListItem.Update();
    newPage.Update();
    newPage.ListItem.File.CheckIn("System Checkin");
    newPage.ListItem.File.Publish("System Published");
}
Was it helpful?

Solution 2

I overlooked the fact that I had to turn on the "Site Feature" so that webprovisioned event receiver kicks in. There were no bugs to fix.

Thanks everyone.

OTHER TIPS

Here's the part that it looks like is missing from your code:

// Create a list of page layouts
ArrayList newPageLayoutList = new ArrayList();
foreach (PageLayout pl in pageLayouts)
{
  if (pl.Name.Equals("RMLayout.aspx", StringComparison.InvariantCultureIgnoreCase))
  {
    newPageLayoutList.Add(pl); // add your pagelayout to this list
  }
}

// Assign your new list as the available page layouts
publishingWeb.SetAvailablePageLayouts(newPageLayoutList.ToArray(), false);
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top