Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / operating-systems / Windows

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

0.00/5 (No votes)
14 Dec 2011CPOL 10.6K  
How to use SharePoint Feature to Activate and Deactivate Custom SharePoint Master page

The below example only sets the Master page URL, but not the custom master page URL. thus Layout pages will be the same after you deploy the master page.  You can also change the Custom master page URL.

Feature Activation

C#
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)

C#
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();
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)