3 out of 10 rated this helpful - Rate this topic

Delegate Control (Control Templatization)

Windows SharePoint Services 3

Windows SharePoint Services 3.0 implements a straightforward mechanism to declare candidate controls for a particular functionality and provides a mechanism for choosing a single functionality provider. Through a Feature you can identify controls and control templates that serve as candidates for a chosen functionality.

Controls are identified based on the functionality that is provided by the control. You can register controls for these functionalities through Feature elements. You specify which sort of control to use, a sequence number, and a URL to either a control template or an assembly, as shown in the following code examples:

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control 
    Id="SmallSearchInputBox" 
    Sequence="100" 
    Url="/templates/mysearchcontrol.ascx"/>
</Elements>

The following control replaces the previous control because it specifies a lower sequence number.

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control 
    Id="SmallSearchInputBox" 
    Sequence="50" 
    ControlClass="MySearchControl" 
    ControlAssembly="MyAssembly"/>
</Elements>

The DelegateControl is embedded inside Windows SharePoint Services pages and acts based on this information, as follows:

<SharePoint:DelegateControl ControlId="SmallSearchInputBox"/>

At run time, this control accepts the union of control elements declared at the server farm, Web application, site collection, and Web site levels. The control that has the lowest sequence number is added to the control tree by means of the DelegateControl. In the case of a sequence tie, the order of controls is arbitrary.

The sequence number of the DelegateControl can be used to integrate a portal search control in Windows SharePoint Services. The default search control has a sequence number of 100, and a portal search, for example, could be activated at the site collection level with a sequence number of 50. In this way, Windows SharePoint Services replaces the default search control with the portal search control in all places where the search control is invoked.

For example, the following example shows the SmallSearchInputBox delegate control:

<SharePoint:DelegateControl runat="server" ControlId="SmallSearchInputBox"/>

This control might instantiate a custom control candidate like the following:

<MySearch:SuperSearchControl/>

The parent delegate control sets properties on the custom control. Use the Property element within DelegateControl to set properties of associated custom controls, using reflection.

Note Note:

Setting the AllowMultipleControls property to true causes all matching candidate controls to be added to the page as children of the delegate control according to the order specified by the Sequence attribute.

A delegate control is not inherently designable because it is ignorant of the actual control that gets instantiated inside it. The best it can do is render the design-time HTML of the chosen control for the particular instance. At best, the designer provides an option to "hardcode" the control, meaning replace the SharePoint:DelegateControl with the current control that is being returned via the Features infrastructure. The developer can then customize the control.

Possible scopes:

  • Farm: Yes

  • Web Application: Yes

  • Site Collection: Yes

  • Web Site: Yes

See Also

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
First sample XML is incorrect
I just burned nearly a full day messing around with user controls and publishing layouts thanks to the error in the first XML sample.  Not only should the attribute be ControlSrc and not URL, but the path is incorrect.  Using "/templates/<control>.ascx" will provide nothing be headaches.  Instead, the path should be "~/_controltemplates/<control>.ascx".  Thanks SharePoint 2007 team...

Here's an even bigger question for you... why is it necessary to create a DelegateControl, which is nothing more than a template, for a user control, which is nothing more than a template?!  User controls in ASP.NET provide a way of easily reusing markup and business logic in multiple locations with the context of one or more web applications.  So why does SharePoint 2007 complicate this by requiring that developers wrap this reusable control in another template? 

Has it occurred to anyone that all the added complexity associated with SharePoint development forces developers to relearn all of the paradigms associated with standard ASP.NET development?
Incorrect attribute: Url
The Url attribute listed for the Control element is incorrect. Instead, it should be ControlSrc attribute.