SharePoint 2010: Delegate Controls in Action

We can following noteworthy points on Delegate Controls in SharePoint:

  • SharePoint provides a mechanism that allows developers to customize out-of-the-box functionality in pages, without the need for modifying the pages that come with SharePoint. This mechanism is provided with delegate controls.
  • A delegate control defines a region in an aspx page that allows the content to be replaced with our custom content. This custom content is created via a SharePoint feature, and when deployed and activated, replaces the standard content at runtime.
  • By declaring a control as an element in a Feature and giving it a priority through the Sequence attribute of the Control element, SharePoint selects the declared control candidate and instantiates it as a child of the delegate control. At run time, this delegate 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 through the DelegateControl.
  • Delegate controls for which the AllowMultipleControls property equals true can host more than one child control.
  • If there are multiple delegate control candidates, they are all added as children at runtime in the order specified by the Sequence attribute. For example, by default the AllowMultipleControls property is used in the AdditionalPageHead delegate control to allow multiple child controls to be added to any page that contains the delegate control.

Now let see the delegate control in action.

In this article we will see the steps involved in replacing the SharePoint global navigation with Control ID “GlobalNavigation”. We will discuss the significance of this Control ID attribute in detail later in this article.

Project Structure:

1

Based on the above project structure following are required artifacts to be developed in order to get the delegate control working:

  • Custom Control   :          GlobalNavigation.ascx
  • Module                :          GlobalNavigation
  • Web Scoped Feature :  CustomDelegateControl

Custom Control: First of all we need to create a custom control that will be used to replace the existing SharePoint Delegate Control.

Definition of GlobalNavigation.ascx

2

GlobalNvaigation.ascx file contains the UI logic for the delegate control, in this case this is simply declaring a label control that will contains the urls of all lists in current site.

Definition of GlobalNavigation.ascx.cs

3

GlobalNavigation.ascx.cs file contains the logic to display all the lists from current site.

Element.xml

4

The information in Element.xml file is very important to understand, as this information tells SharePoint which Delegate Control needs to be replaced.

  • Id Attribute contains the value of the OOB SharePoint Delegate Control, make sure you specify correct value, else this won’t work.
  • Sequence Attribute contains the value of the sequence in the control tree, make sure this value is less than the sequence value for OOB SharePoint Delegate Control else OOB SharePoint Delegate Control won’t get replaced by the custom control.
  • ControlSrc Attribute contains the path of the custom control relative to the ControlTemplates Directory under SharePoint Root Folder (14 Hive).

Web Scoped Feature:

5

Now we are done with all the deployable, and it’s time to create a feature which will deploy the artifacts in SharePoint Farm.

We need to create a Web Scoped feature, which allows us to activate this Global Navigation Control on the specific sites instead of the complete site collection, though this can be designed based on the business requirements.

Now build the solution and deploy.

Final Output:

6

And we are good to go.

Hope this will help someone in need…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s