R – How to force a new site collection to inherit a master page

master-pagessharepointwss

I have some code that creates a new site in SharePoint. Upon browsing to the newly created site, a File Not Found error is thrown. If you browse to /_layouts/ChangeSiteMasterPage.aspx you can select a new site master & system master page, and the site works fine. This kb article describes my symptoms perfectly: http://support.microsoft.com/kb/936908

My problem is that of the two (maybe three?) solutions given, only one works. If I manually select the new master pages it works fine. The second workaround is to activate the publishing feature on the new site. This does not fix anything.

There is also a recommendation to staple the publishing feature to the site definition I am using for the new site. In my case, this is STS#1 (the blank site), and stapling the publishing feature does not alleviate my problem.

Anyone have an idea of how I can get the correct master page sorted out?

Best Answer

We do this through a delegate control. In the OnLoad we call the following method:

private void ConfigureMasterPage(SPWeb web)
{
       string masterURL = string.Empty;
       masterURL = web.Site.ServerRelativeUrl + "/_catalogs/masterpage/XXX.master";
       masterURL = masterURL.Replace("//", "/");
       web.MasterUrl = masterURL;
       web.CustomMasterUrl = masterURL;
       web.Update();
}

Dont forget to do a dispose of the SPSIte and SPWeb objects and You will have to set AllowUnsafeUpdates to true.

Hope this helps