Wednesday, December 14, 2011

How to use SharePoint Feature to Activate and Deactivate Custom SharePoint Master page

Below example only set the Master page Url but not the custom master page URL. thus Layout pages will be same after you deploy the master page.  You can also change the Custom master page URL also.

Feature Activation

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;

string urlMaster;
string urlCustom;

if (@"/".Equals(web.ServerRelativeUrl))
{
urlMaster = @"/_catalogs/masterpage/BLLICT.master";
urlCustom = @"/_catalogs/masterpage/v4.master";
}
else {
urlMaster = string.Concat(web.ServerRelativeUrl , @"/_catalogs/masterpage/BLLICT.master");
urlCustom = string.Concat(web.ServerRelativeUrl , @"/_catalogs/masterpage/v4.master");
}

web.MasterUrl = urlMaster;
web.CustomMasterUrl = urlCustom;

web.Update();

}

Feature Deactivation (Reverse to Original Master)

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
string urlCustom;

if (@"/".Equals(web.ServerRelativeUrl))
{
urlCustom = @"/_catalogs/masterpage/v4.master";
}
else
{
urlCustom = string.Concat(web.ServerRelativeUrl, @"/_catalogs/masterpage/v4.master");
}

web.MasterUrl = urlCustom;
web.CustomMasterUrl = urlCustom;

web.Update();
}
Post a Comment