How to deploy SharePoint Master Page as a Feature


I am under the assumption we already have a custom master page designed and we are trying to convert it to a feature. The MasterUrl and CustomMasterUrl attributes represent two replaceable parameters that can be used to set the Masterpage attribute of the @Page declaration on the pages of a SharePoint site.  By default  .aspx pages point to a Master page at ~/masterurl/default.master. This will be replaced at runtime by the MasterUrl property of the Website.  The default setting for this is _catalogs/masterpage/default.master.  This is the default.master file in the Master Pages gallery of the website.  Until you edit this file using SharePoint Designer it will point to a ghosted copy of the file in the TEMPLATE\GLOBAL directory of the 12 hive.  You can also ghost custom .Master files from your Site Definition directory into the Master page gallery of the site using a Module Element.  Then you can set the MasterUrl and/or CustomMasterUrl attributes of the Configuration Element in the ONET.xml file to point at these ghosted .Master files.  If you also change the @Page directive of the .aspx files on your site to use ~/masterurl/custom.master you will establish a hierarchy of master pages that will be used.  The page will first try to use the customMasterUrl setting, then the masterUrl setting, and finally the default.master file in GLOBAL.

Create Feature folder with Elements.XML, feature.xml, Master Page folder containing the Master Page.

master.png

Feature.XML file can be found below. Set the Scope of the feature to the site collection.

Feature1.png

Element.xml file


Feature2.png

Now we need to create an event handler for feature activated, deactivated event. Place the event handler dll in GAC.This is for the purpose when we activate this feature, our custom master page should set as the default master for our site. Similarly when we deactivate the feature we need to set the default. Master as our default master page

Feature Activate Event

Event1.png
 

Feature Deactivate Event

Event2.png

Mention the event handler key, namespace inside feature.xml file

Deploy the solution

Activate the Feature from site collection features. If site exists with the name specified in the event handler code then the specified master page get attached to the site. While deactivating the master page of the site get changed to default master page.

Featurelast.png
 

Before retracting the solution, the feature should be deactivated.