Complete with source code.
So you want to customise your site. You create a master page; you even create your own theme. Just look at your perfect, beautiful site. Then you realise that every time you view a page in the layouts directory your master page is ignored.
This is because all these pages use the /_layouts/application.master master page. So how does one change this setting? In short the answer is you don't. Microsoft do describe two methods for
cusomising application pages in the Layouts folder but both are lacking. Each method has advantages and over the other but neither satisfy my requirements.
I'd like to be able to
- specify an alternate master page for any site (SPWeb) and have all sub-webs inherit the master page
- configure the url to the master page via the UI
- still use the uncustomised version of the application.master file
There are several components to the solution of this issue which can be download at the end of this post (including source code).
Changing the Master PageIn order to change the master page we need a way to check every page requested to see if we need to change the master page. To do this we will use a
HttpModule. This will allow us to inspect the page being requested and if required attach an event to modify the master page.
The HttpModule will get the page handler and check the MasterPageFile property to see if it is set to the application.master. If it is, it will check the current web's property bag to see if an alternate master page has been specified. If no alternative is set, it will check the parent webs and inherit the setting. If an alternate is found an event handler will be attached to the page's init event that will then change the master page.
Configuring the Master PageA custom action will be used to create a link in the Site Administration section of the site settings page. This will load a new application page that allows a site admin to set the alternate url for the master page that will be stored in the current web's property bag.
Sticking it all togetherNobody likes fixes that require manual changes to the file system and to the web.config files, so the aim is to create a single web application feature the can be installed, deployed and activated. The HttpModule requires modifications to the web.config file, this will be handled via a
feature receiver that applies the
web config modifications.
The feature will also install the application page that allows the site administrator to modify the URL to the alternate master page and add a link to the page into site settings under the Site Administration section.
To try this solution download the SharePoint solution file and install and deploy it on your farm. In Central Administration go to Manage Web Application Features and activate the Application Master Page feature on the required web application. After doing this if you go to the site settings page of any site on the web application you will see an Application Master Page link. Change the url to the location of your new master page.
Finally the Master PageTo test this solution you may wish to simply copy the application.master and make a small alteration so that you can see the difference. My recommendation for a full solution would be to create master pages using the application.master file as a template and deploy them to the layouts folder as part of your solution.
Download the solution here
seed.hf2.wsp.
Download the source code here
seed.hf2.zipPlease leave a comment if you found this post helpful or if you have any suggestions on how to improve it.