65.9K
CodeProject is changing. Read more.
Home

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

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Dec 14, 2011

CPOL
viewsIcon

10570

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

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