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…

SharePoint 2010: Connected Web Parts in action

Connected WebParts are best suited for the scenarios where data communication between two WebParts is required at run time.

Following are the most basic steps involves in the development of Connected Web Parts:

Step1 : Creating WebPart connection interface

Step2 : Creating Provider WebPart using WebPart Project Item

Step3 : Creating Consumer WebPart using WebPart Project Item

Now lets see the connected WebPart in action.

  • Create Empty SharePoint Project

1

2

  • Creating WebPart connection interface : Add a class file which contains the definition of the interface.

3

  • Interface Definition

4

  • Creating Provider WebPart using WebPart Project Item : Add new project item based on WebPart project item type

5

6

  • Provider.cs: This file implements the business logic for Provider WebPart. This class will implement ConnectionProvider Property

78

  • Element.xml: This file will expose the deployment information of Provider WebPart to SharePoint

9

  • Creating Consumer WebPart using WebPart Project Item

10

11

  • Consumer.cs: This file implements the business logic for Provider WebPart. This class will implement ConnectionConsumer Property

1213

  • Element.xml: This file will expose the deployment information of Consumer WebPart to SharePoint
  • Creating Site Level Feature for the deployment of connected WebParts

14.1

  • Build and Deploy solution
  • Test Connected WebParts
  •      Create a new WebPart Page

15

16

17

  •      Add Provider and Consumer WebParts on the WebPart Page

18

  •     Create connection between Provider and Consumer WebParts

19

20

  •    Enter string in the Provider WebPart and click Send Button, the string value is transferred to the Consumer WebPart using connection between Provider and Consumer WebPart.

21

  •  Value transferred to Consumer WebPart on button click.

22

Hope this will help someone in need…