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…)
[…] So, can we customize them? Yeah, no reason why not – either, edit the MWSDefault.master for a site in place (yuck) or create a feature to deploy a new master page and set the CustomMasterURL in code, similar to what I’ve done before with team sites. […]
[…] built a feature to active branding on a site including master page, navigation and themes, which I’ve been talking about a bit over the last few weeks. One […]
[…] you can activate a new master page in a Feature Receiver. And we can detect the type of site we’re dealing with. Why not combine the two […]
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
Have you put the property key ‘MasterPageFile’ in the feature.xml file? Otherwise it should at least break the page!