I have used this code on many projects for 2010 on a site-scoped feature, and it has always worked:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
if (properties != null)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPWeb _web = ((SPSite)properties.Feature.Parent).RootWeb)
{
Uri _siteMaster = new Uri(string.Format("{0}/_catalogs/masterpage/CustomMaster.master", _web.Url));
Uri _siteSystemMaster = new Uri(string.Format("{0}/_catalogs/masterpage/CustomMaster.master", _web.Url));
_web.CustomMasterUrl = _siteMaster.AbsolutePath;
_web.MasterUrl = _siteSystemMaster.AbsolutePath;
// Use these properties to control the radio buttons
_web.AllProperties["__InheritsCustomMasterUrl"] = "false";
_web.AllProperties["__InheritsMasterUrl"] = "false";
_web.AllProperties["__InheritsAlternateCssUrl"] = "false";
_web.Update();
}
});
}
}
But in 2013, it doesn't seem to work, meaning by activating the feature, the master doesn't get set in Site Settings -> Masterpage. I have debugged the feature, and all is well, so I'm not sure if this is just an issue for me, or if anyone else has run into the same issues. I'm not finding anything in the community about it, so I wanted to start the discussion here.
I should also mention that the site collection root web is based on a Publishing Portal site template with all subwebs inheriting their master.
Thanks in advance for taking a look!