SharePoint 2010 Branding: Invalid site master page in non-publishing sites

If you create a new non-publishing site you will get a master page error ("The site master page setting currently applied to this site is invalid. Please select a new master page and apply it") and your site will default to the v4.master. This only happens when the root site is a publishing site. It's also because the root site is a publishing site that you can navigate to the master page settings page via the url /_layouts/changesitemasterpage.aspx.

This might be the reason why you can't create a non-publishing site under a publishing site by default... But people still need and want to use those site templates, so I had to find a solution for it.

If you're using the standard master pages you won't see any problem since the master page will default to the v4 anyway. If you're using a customized master page the problems will be visible because the inheritance of the Alternate CSS will still work, the master pages won't.

When you manually activate the inheritance in the settings page the error is gone and the look and feel are fine, but I can't have every client go through this manual process every time they want to create a non-publishing site. So I created a feature to activate the inheritance automatically. I also use stapling to automatically activate the inheritance feature for each site template I need, and I can reuse this mechanism in every project.

Inheritance feature

This has to be a Web scoped feature. The most important part of this feature is in its event receiver.  The code is clear, we're activating all three inheritance properties we see in the master page settings interface.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = (SPWeb)properties.Feature.Parent;
    
    if (!web.IsRootWeb) 
    {
        Hashtable hash = web.AllProperties;
        web.MasterUrl = web.ParentWeb.MasterUrl;
        hash["__InheritsMasterUrl"] = "True";
  
        web.CustomMasterUrl = web.ParentWeb.CustomMasterUrl;
        hash["__InheritsCustomMasterUrl"] = "True";
  
        web.AlternateCssUrl = web.ParentWeb.AlternateCssUrl;
        hash["__InheritsAlternateCssUrl"] = "True";
  
        web.Update();
    }
}

Stapling feature

In this feature we associate the Inheritance feature to the site templates we wish. See bellow an example for the Team Site template.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
    <FeatureSiteTemplateAssociation Id="00000000-0000-0000-0000-000000000000" TemplateName="STS#0" />
</Elements>
Dércia Silva
Posted by Dércia Silva on March 12, 2012

Related articles