Site Feature for Custom Master Page in SharePoint 2010

If you were trying to develop a feature and have a master page deployed in your system and want to make the site feature to be applied for the custom master page…

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite siteCollection = (SPSite)properties.Feature.Parent;
SPFeatureProperty masterUrlProperty = properties.Feature.Properties[“MasterPage”];
string masterUrl = masterUrlProperty.Value;

if (String.IsNullOrEmpty(masterUrl) == false)
{
masterUrl = SPUrlUtility.CombineUrl(siteCollection.ServerRelativeUrl,
“_catalogs/masterpage/” + masterUrl);

foreach (SPWeb site in siteCollection.AllWebs)
{
// site.MasterUrl = masterUrl;

site.CustomMasterUrl = masterUrl;
site.Update();
}
}
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite siteCollection = (SPSite)properties.Feature.Parent;
string masterUrl = SPUrlUtility.CombineUrl(siteCollection.ServerRelativeUrl,
“_catalogs/masterpage/v4.master”);

foreach (SPWeb site in siteCollection.AllWebs)
{
// site.MasterUrl = masterUrl;
site.CustomMasterUrl = masterUrl;
site.Update();
}
}

 

 

site MasterUrl will change the master page for the entire system which includes the site pages, application pages and system pages.

CustomUrl will change the custom master page

Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami