NOTE: This is a re-post from my orginal blog post on DevCow - SharePoint Delegate Controls in SharePoint 2007 - Best Practices
SharePoint Delegate Controls: What EVERY SharePoint developer should know, but most do not.
Every time I talk to someone about SharePoint 2007 development, I ask them if they know about SharePoint delegate controls. Almost all of them have said no at first. LEARN THEM!
I feel this is one of the most important features of SharePoint 2007 if you are doing any type of customization or skinning.
A delegate control is user control that has a predefine space on a page that can be changed by turn on or off a feature. The stock example used is, if you want to change the search area you could upload a feature with a user control to replace it with your own. Delegate controls are based on numbered sequences with the lowest being displayed. Yours just has to be the lowest.
Looking on the web there are some good resources to get you started
- Using Delegate Controls in Sharepoint
- Delegate Control (Control Templatization and Lightup)
- Using the Delegate Control
A couple rules of thumb that I use, but some have no real technical reason. These are just some best practices for delegate controls:
- When designing your page, create delegate controls for functionality that might change over time. (i.e. the search box like Microsoft did)
- Don't over use delegate controls. You will find once you understand them you will want to use them everywhere.
- Number 3 is my most important rule, you should have 3 delegate controls on every page.
- Don't put too many delegate controls in a single feature, remember they all deploy together. (light up/turn off together)
- Keep good track of the sequence and don't put the number too low. You don't want to have trouble overriding the feature in the future.
Most people always give me a hard time about number 3 because there are no technical reasons to have 3 delegate controls on a page. The reason I think this rule holds true is because on most pages you will have a search area (1 delegate control), navigation data source (2 delegate controls), and then you might have your own for the functions of your application (3 delegate controls). Every application will be different, but I think you will find that in your application you will have 3 or more delegate controls per page.
Here are some areas that could use some improvement going forward with delegate controls.
- A user interface page in the site settings to manage delegate controls for the site.
- The ability to create a delegate control container on the fly out of the box.
- Adding a new control ID to a site from the user interface. (i.e. <Control Id="ControlArea" Sequence="25" ControlSrc="~/_ControlTemplates/AddedControl.ascx" />)
- Have the ability to change the sequence number of the loaded delegate controls on the fly though the user interface.
- Be able to change a delegate control sequence per site within the user interface.