1

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");
}
2
  • Does the code execute all the way through? Can you be sure that currPageLayout` is not null when you create newPage?
    – James Love
    Commented May 18, 2011 at 19:19
  • In dev it works perfectly.
    – Roxanne
    Commented May 18, 2011 at 19:35

2 Answers 2

1

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);
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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