I found the following steps have worked well for me, thanks to sssreddy for the feature stapling links.
- Create a feature scoped at "Web - Activates a Feature for a specific web site".
- Add an event receiver to the feature, and implement the
FeatureActivated and FeatureDeactivating methods, setting the SPWeb.MasterUrl (and if you want to enable publishing features later, the SPWeb.CustomMasterUrl) properties of the feature's parent SPWeb1.
- Create another feature scoped at Site, Web Application or Farm, as preferred for your management requirements.
- Add an element to this feature that defines which features you want to enable on which site templates2.
1 The code in my FeatureActivated method looks something like this:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// Get the SPWeb we're being activated in.
var web = (SPWeb)properties.Feature.Parent;
if (null != web)
{
// Get the Site Collection root path to get the master page gallery.
string siteCollectionRoot = web.Site.RootWeb.Url;
// Set the Site Master to Custom.master
var siteMaster = new Uri(siteCollectionRoot +
"/_catalogs/masterpage/" +
"Custom.Master");
web.CustomMasterUrl = siteMaster.AbsolutePath;
// Set the System Master to Custom.master
var systemMaster = new Uri(siteCollectionRoot +
"/_catalogs/masterpage/" +
"Custom.Master");
web.MasterUrl = systemMaster.AbsolutePath;
// Clear the Alternate CSS
web.AlternateCssUrl = string.Empty;
// Save the changes back to the web
web.Update();
}
}
The FeatureDeactivating method was similar but obviously set the master pages back to V4.master.
2 My Elements.xml file was fairly simple:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<FeatureSiteTemplateAssociation Id="e5f2c515-d45f-4096-a0b1-7d486a4d011a"
TemplateName="GLOBAL" />
</Elements>
As you can see, I set this to hook into every Site Template, and had some code in my feature receiver to filter out certain templates (i.e. Blogs) where I needed to use a slightly different set of master pages.
More indepth steps can be found on my blog post.