SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;
   PublishingPageCollection pages = publishingWeb.GetPublishingPages();
   PublishingPage newPage = pages.Add(pageName, currPageLayout);
   string content = "";  // "Welcome to <strong>My Page</strong>";
   newPage.ListItem[FieldId.PublishingPageContent] = content;
   newPage.Title = strPageTitle;

    newPage.ListItem.File.CheckIn("System Checkin");
    newPage.ListItem.File.Publish("System Published");
share|improve this question
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

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);
share|improve this answer
up vote 0 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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