Click here to Skip to main content
15,889,216 members
Articles / Web Development / ASP.NET

SharePoint 2010 Master Page Customizations Through Code

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
28 Sep 2010CPOL2 min read 24.8K   406   11   1
Ever been in a situation where you need to modify a SharePoint master page, but really would like not having to create a custom master page? It can be done. Sure, this depends on the amount of customizations, but in a lot of situations you can do what you need from code.

Ever been in a situation where you need to modify a SharePoint master page, but really would like not having to create a custom master page? It can be done. Sure, this depends on the amount of customizations, but in a lot of situations you can do what you need from code.

You might have heard about delegate controls. Standard SharePoint master pages have a few of them, so if you want to do things like replace the data source for the main menu, or replace the global search, that is there for you ready to use. But say you want to do something that SharePoint has not provided a delegate control for. For example, if you need to display additional menu levels. Then you have at least three options, either create a custom master page, customize the existing or utilize the delegate control “AdditionalPageHead”.

To achieve this using the AdditionalPageHead delegate control, you have to do the following; first create the code to modify the master page, then attach this to the delegate control on the master page. For this example, I have created a Visual Studio 2010 SharePoint Solution. It consists of one feature “MainMenuDisplayLevels” which contains the "AdditionalPageHeadDelegates” element.

The AdditionalPageHeadDelegate in turn attaches the custom control “MainMenuLevelsCustomizer” to the AdditionalPageHead delegate control.

The web control itself is basically just locating the menu control and modifying its properties.

One note though. In order for this to work, you need to register the assembly containing the control as safe. In order to do that, you need to do two things. First, change the project properties so that it’s not included in the solution package.

Then change the package so that it includes the project and registers it as safe.

So why go through all of this just to do a simple customization? Probably the most important reason for doing this is forward compatibility. This will reduce the complexity in upgrades. The less UI changes you make, the more safe you can be about SharePoint taking care of upgrading your site. It will also simply reuse your code on other SharePoint sites since you are not depending on custom master pages. You might be required to write some code in your control in upgrade scenarios, but that is a small change compared to having to upgrade custom master pages to a next version of SharePoint.

So if you have requirements like requiring to run custom code on any page, or doing a minor modifications to the master page, then I really recommend looking into the AdditionalPageHead delegate control.

Turned out WordPress is not allowing upload of zip files, so if interested in the full solution for this example, send me a mail.


License

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


Written By
Technical Lead Software Innovation
Norway Norway
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAwesome work Pin
sangeet.ahuja17-Mar-11 18:54
sangeet.ahuja17-Mar-11 18:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.