1

When building a SharePoint 2010 site, we have a root Publishing site that has been branded up using a custom Master Page.

We'd now like to add some Team Sites below the publishing site, and have our branding and other look and feel changes applied there too, and for that we need to change the "System Master" setting of the Team Site.

The "Master page" options in the site settings are only available if I've enabled the Publishing features in the team site, but I'd rather not do this if I can avoid it.

Ideally, this should be enabled as soon as a new team site is created, without the site owner having to change the site settings.

4 Answers 4

2

You can also use feature stapling. Take a look at this

1

The publishing features will provision a link in site settings, but that is not required to change the masterpage via code.

There is a WebProvisioned event that you can use to change the masterpage settings of the SPWeb object.

0

I found the following steps have worked well for me, thanks to sssreddy for the feature stapling links.

  1. Create a feature scoped at "Web - Activates a Feature for a specific web site".
  2. 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.
  3. Create another feature scoped at Site, Web Application or Farm, as preferred for your management requirements.
  4. 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.

0

Ideally you can do it via PowerShell:

$web = Get-SPWeb "[target site Url]"
$web.MasterUrl = "/_catalogs/masterpage/Custom/mycustom.master"
$web.Update()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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