Tuesday, 2 December 2014

Make Your SharePoint 2010 Master Page Extensible with Delegate Controls


I have been playing a lot the last little while with SharePoint 2010 Delegate Controls. I have known about them for a ling time, but have never really delved into them all that much.
Most of the examples I have looked at, and the usage ideas I have seen, involve using the existing delegate controls in V4.master to do things like:
  • Modify the Welcome Menu
  • Customize the Global Navigation
  • Add useful code to the page header
Last week, I used a delegate control for something a little different.
For a project I am working on, the client wanted some functionality displayed just below the Quick Launch, on every page in the site collection. I know there are lots of ways to do this (put a user control on the master page, put a web part on the master page, or just put links on the master page, since that’s all the content was in this case).
But then I thought of a little bit more elegant (to me) solution. Rather than explicitly putting the content on the master page, I added a delegate control of my own to the page, and placed it just below the Quick Launch, as shown below.

http://fyeomans.files.wordpress.com/2011/05/image6.png

Several things to note here:
  • I gave it a ControlID specific to my application;
  • I set AllowMultipleControls to true, which will be useful later
  • I included default content in the <Template_Controls> element, so that I could see it change when I activate a Feature with an appropriate control.
  • Next, I implemented a Feature in Visual Studio 2010, with a control to replace the default content. The details of how to do this are covered in other places (such as here), but to summarize:
  • I created an empty Visual Studio 2010 project;
  • I created a user control inside the project (this automatically mapped the ControlTemplates folder for me). It is a very simple user control, and simply displays “Hello, Delegate!”
  • I created an appropriate elements.xml file to map my control to the Delegate Control’s ControlID.
So, here we see the how the delegate looks before I deploy my feature:


http://fyeomans.files.wordpress.com/2011/05/image.png


After I deploy my Feature, we see that the default content is replaced with the Feature’s control’s output:

http://fyeomans.files.wordpress.com/2011/05/image1.png


But wait, there’s more! Remember that I set AllowMultipleControls to true? That allows me to deploy multiple features with controls that map to my ControlId, and instead of only displaying the one with the lowest Sequence attribute, it will stack them all in order of Sequence number, as shown below.


http://fyeomans.files.wordpress.com/2011/05/image2.png


This means that I can add any number of  things to the area under the Quick Launch without further customizing the the Master Page.
Maybe I am just easily amused, but I thought that was pretty neat!
The source for this, including the master page, is available here.



Reference:

http://fyeomans.com/2011/05/16/make-your-sharepoint-2010-master-page-extensible-with-delegate-controls/


No comments:

Post a Comment