When deploying your SharePoint site you probably are using an automated script that will provision all of your webs and features. If your site has custom master page you can apply the setting for it inside your site template ONET.xml. In some cases you may want to set your masterpage based on a custom logic. To do that you can create a feature which will set your masterpage through a custom receiver along with whatever logic you may have.
Here is how your Feature.XML would look like:
<?xml version=”1.0″ encoding=”utf-8″?>
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/”
Id=”D97DE9A1-8D5B-4745-9B12-41D51F808601″
Title=”MyProject : Set System Master”
Description=”MyProject: Set System Master”
Version=”1.0.0.0″
Scope=”Web”
Hidden=”False”
ActivateOnDefault=”FALSE”
AlwaysForceInstall=”TRUE”
ReceiverAssembly=”MyProject.Platform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0bcd2fea03aa1111″
ReceiverClass=”MyProject.Platform.SetSystemMaster”>
<ElementManifests>
</ElementManifests>
</Feature>
Now here is how my receiver class will look like:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using (SPWeb web = properties.Feature.Parent as SPWeb)
{
if (properties.Feature.Properties["MasterUrl"] != null)
{……………..
web.AllowUnsafeUpdates = true;
web.MasterUrl = properties.Feature.Properties["MasterUrl"].Value;
web.Update();
}
}
}
And here is how you’d call feature like this in your one during site provisioning:
<Feature ID=”D97DE9A1-8D5B-4745-9B12-41D51F808601″>
<Properties xmlns=”http://schemas.microsoft.com/sharepoint/”>
<Property Key=”MasterUrl” Value=”/_catalogs/masterpage/MyCustom.master”/>
</Properties>
</Feature>
Here I have just one property, in your case you may want to pass more properties that will be used in your receiver logic to determine what masterpage are you going to apply in which case. That’s it!