Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

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

, 14 Dec 2011
Rate this:
Please Sign up or sign in to vote.
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();
}

License

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

Share

About the Author

Melick

Sri Lanka Sri Lanka
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 14 Dec 2011
Article Copyright 2011 by Melick
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid