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:
<Property Key="MasterPageFile" Value="mymaster.master"/>
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 ;
//Update Master Pages to new URL
site.CustomMasterUrl = site.ServerRelativeUrl + "/_catalogs/masterpage/" + masterFile.Value;
site.MasterUrl = site.ServerRelativeUrl + "/_catalogs/masterpage/" + masterFile.Value;
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…)