Click here to Skip to main content
15,885,546 members
Articles / Operating Systems / Windows

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
14 Dec 2011CPOL 10.5K  
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)


Written By
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --