Setting the Master Page of a Team Site with a Feature

Team sites don’t have the Publishing features of MOSS enabled by default, and for WSS systems, well, you don’t have them to enable. Consequently, if you deploy a master page as a feature, you’ll have a bit of fun setting it as the master page.

You can do this though SharePoint Designer, but it’s possible you don’t want your Site owners to have SharePoint Designer, or maybe you just don’t want the hassle of the second step when you provide the master page for a site. Instead, it is possible to set the Master Page through code, and to fire this using a Feature Receiver. This features shows just that – setting the master page through code. Note that to install it, you might well need to make some changes to install.bat.

So how does this work? Well, the feature.xml file refers to a feature receiver SetMaster.FeatureReceiver (catchy name, eh?) It also defines the name of the master page that we’re installing as a property:

<Properties>
<Property Key="MasterPageFile" Value="mymaster.master"/>
</Properties>

This property will be used by SetMaster.FeatureReceiver to determine the correct master page to use.

The code itself is pretty straight forward, if you look inside FeatureReceiver.cs:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb site = properties.Feature.Parent as SPWeb;
SPFeatureProperty masterFile = properties.Definition.Properties["MasterPageFile"];
if (masterFile != null)
{
//Save original Master Page URL
site.Properties["OriginalMasterURL"] = site.MasterUrl;
site.Properties["OriginalCustomMasterURL"] = site.CustomMasterUrl ;
site.Properties.Update();
//Update Master Pages to new URL
site.CustomMasterUrl = site.ServerRelativeUrl + "/_catalogs/masterpage/" + masterFile.Value;
site.MasterUrl = site.ServerRelativeUrl + "/_catalogs/masterpage/" + masterFile.Value;
site.Update();
}
}

Yup, all we do is get the master page name from the feature’s properties, save the URLs to the original master pages, and update the master page URLs. And yes, there are two of them, and no, I’m not entirely sure what the difference is. If anyone knows, I’d be interested.

Feature deactivation simply does the reverse.

If you dig into the other files in the manifest you’ll see that I also install an image and a CSS file, but these aren’t used in the master page we’ve installed. Unfortunately, I’ve yet to figure out how to do relative URLs that are WSS friendly, and this solution should work with WSS as is (though I’ve only tested on a MOSS system…)

Advertisement
Setting the Master Page of a Team Site with a Feature

5 thoughts on “Setting the Master Page of a Team Site with a Feature

  1. franck says:

    Hi,

    thanks for what you did.

    when i activate the feature to a site collection (team site), the master page is uploaded to the site into the master page gallery but it’s not applied automatically. any idea?

    thanks

  2. Have you put the property key ‘MasterPageFile’ in the feature.xml file? Otherwise it should at least break the page!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.