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

| improve this answer | |

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.