Fun MOSS Quirks: Custom Site templates PageLayoutURL

Posted: November 15, 2007 in MOSS 2007

       This particular one will nearly drive you to suicide. So you develop a sweet site and decide, hey, I am going to save this out to a site template (stp) file and deploy it out to my other MOSS farms. You got a nice welcome page that is created with a custom page layout you developed. You include your stp file(s), custom master pages, custom CSS, images, custom page layout(s) and other branding features in a wsp and deploy them to your other MOSS farm and activate the features. Everything is nice, then you create your first site using one of the templates and all hell breaks loose. Your custom welcome page will not load, in your researching you take a look at your page layout and associate content type. It is then, and only then you find…your page layout on your other server farm is piinting back to the original site you developed on. So what the heck is going on?
      What’s going on is a field in MOSS called "pageLayoutURL". If is found via the page.ListItem[FieldiD.PageLayout] call. It contains a freaking fully qualified URL to the original site "http://" and all. Apparently, this field passed MS’ testing process. It can create some serious issues for your site.
      As part of any feature rollout, I have included some new code to help me out in this. I pulled it off another blog (as soon as I find the URL I will post that fellows URL here so he can get proper credit).
      First you need to create a site using the sp file you pulled over in your feature. Next, you need to be sure publishing is turned on. Next you run the code I will include in this post against that site (it is fugly POC code so if you need to clean it up for your purposes, feel free). Basically, it cycles throught every page in a site and reset this value to the current site URL.  For my feature, I actually created some script that will provision a new site usin gthe template, turn on publishing, execute the fix code, turn off publishing, and save new template from the updated site and blow away the old template.
sample code:
PublishingPageCollection pages = publishingWeb.GetPublishingPages();
            foreach (PublishingPage page in pages)
                string pageLayoutUrl = (string)page.ListItem[FieldId.PageLayout];
                utilities.WriteToLogFile("MOSSFunctions", "FixPages: Now Checking: " + page.Name + " Layout url is at: " + pageLayoutUrl);
                //if pageLayoutURL is null or empty or page layout url index of "/catalogs" < 0)
                if (string.IsNullOrEmpty(pageLayoutUrl) || pageLayoutUrl.IndexOf("/_catalogs/") < 0)
                    utilities.WriteToLogFile("MOSSFunctions", "FixPages: Condition one tripped");
                SPFieldUrlValue url = new SPFieldUrlValue(pageLayoutUrl);
                string temp = publishingWeb.Web.Site.ServerRelativeUrl.TrimEnd(‘/’) +
                temp = publishingWeb.Web.Site.MakeFullUrl(temp);
                if (url.Url.ToLowerInvariant() == temp.ToLowerInvariant())
                    utilities.WriteToLogFile("MOSSFunctions", "FixPages: Condition two tripped");

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s