2

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!

2

A couple quick notes about your code:

  • You do not need to dispose of the RootWeb object.
  • You aren't elevating privileges at all if you cast the SPSite from the event receiver properties. You need to open a new instance of SPSite in the elevated code block, but privilege elevation isn't necessary at all here.
  • Is your master page deployed to each SPWeb? Typically it's only deployed once to the site collection's root master page gallery and pulled from there. If you are duplicating the file across every sub site that is unnecessary.
| improve this answer | |
1

this should do the trick.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
     SPWeb Web = properties.Feature.Parent as SPWeb;

     Web.MasterUrl = Web.Site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/CustomMaster.master";

     Web.CustomMasterUrl = Web.Site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/CustomMaster.master";

     Web.Update();

     Web.Dispose();
}
| improve this answer | |
  • Why would I need to set the masterpage for each subweb? I failed to mention that the site collection is based on a Publishing Portal site template, and all subwebs are set to inherit the master. – TexasT Jan 28 '14 at 16:10
  • just ammended the answer! – Ali Jafer Jan 28 '14 at 16:12
  • Can you confirm this works in the same scenario? (SP 2013 Publishing Portal site collection?) I know the code sample works in 2010. – TexasT Jan 28 '14 at 16:52
  • I tested with the above code and it still didn't work. Can you also confirm that it works on 2013? – TexasT Jan 28 '14 at 19:38
  • I don't think you should Dispose that SPWeb from the event properties. This is an area that isn't covered in Roger Lamb's disposal best practices guide, but the SPWeb from properties.Feature.Parent is akin to SPContext.Current.Web – RJ Cuthbertson Jan 28 '14 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.