Using Delegate Control in Sharepont 2007 / Sharepoint 2010

Using Delegate Control in Sharepont 2007 / Sharepoint 2010
3 votes, 3.67 avg. rating (77% score)

Introduction

Delegate control is one of the best ways for extending the site functionality. Delegate control in master page will be using its default content unless it’s substituted/overridden.

This is same as named placeholder but delegate control can be managed by features which cannot be possible with named placeholders. If the feature is activated substitution code of delegate control will be coming; if deactivated the default value will be coming.



If we open any master page, we can see one of the delegate controls as

<SharePoint:DelegateControl runat=”server” ControlId=”SmallSearchInputBox” Version=”4″/>

“SmallSearchInputBox” refers to the default search text box which will be in the top right corner for the sites.

All the other delegate controls in the master page can be replaced with custom code thus providing greater extensibility.

We will take an example of a scenario, where we can have clear understanding on delegate control and how we can customize it

Scenario

We have a site collection “MyCompany” with two subsites under it “HR” and “Finance”.

In “HR” site, we have to provide a custom search box where as in “Finance” site default search should be used.

Explanation

Our thoughts should not move in providing solution by editing master page or adding script with which the maintenance will be spoiled along with performance. What if we want to get it back to original state after deploying modified master page ; then we end up cursing the design approach we have followed as it involves changing master page and deploying again.

Solution with Delegate control

Replace the default delegate control of search box with a custom search user control and activate the feature only in “HR” site.

So, “HR” site will show you custom search box where as “Finance” site will show you default search box. Later if we want the “HR” site to have the default search;deactivate the feature. I think by now you might understand how useful and powerful than editing master page or adding script

Code

Create a feature “SearchBoxDelegateControl”.

Feature xml will be like the following.

<?xml version=”1.0″ encoding=”utf-8″?>

<Feature xmlns=”http://schemas.microsoft.com/sharepoint/” Title=”SearchBoxDelegateControl”
Description=”Delegate control for search box” Id=”2c380334-88ea-4cb2-a73d-6453cd245329″ Scope=”Web”>
<ElementManifests>
<ElementManifest Location=”SearchBoxElements.xml” />
</ElementManifests>

</Feature>

This above feature.xml is normal feature like any other one with scope “Web” and refers the “SearchBoxElements.xml” file as manifest which is located in the same location as feature.xml file.

SearchBoxElements.xml will be like following

<?xml version=”1.0″ encoding=”utf-8″?>

<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>

<ControlId=”SmallSearchInputBox”ControlSrc=”~/_controltemplates/SearchBoxDelegateControl/SearchInputBox.ascx”

Sequence=”5″ />

</Elements>

As we are inheriting the delegate control “SmallSearchInputBox” of the master page, the Id value of the SearchBoxElements.xml should be “SmallSearchInputBox”.

To be specific, ‘ControlId’ value of delegate control in master page should be equal to the ‘Id’ value in elements.xml.

Now, sharepoint understands that it has replace the delegate control which in the master page.

ControlSrc” is the location of the custom control in the control templates

Sequence” represents the order of the preference, the least value control will override all others.

SearchInputBox.ascx  sample code

######

<%@ Assembly Name=”Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>

<%@ Register Tagprefix=”SharePoint” Namespace=”Microsoft.SharePoint.WebControls” Assembly=”Microsoft.SharePoint,
Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”
%>

<%@ Register Tagprefix=”Utilities” Namespace=”Microsoft.SharePoint.Utilities” Assembly=”Microsoft.SharePoint,
Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”
%>

<%@ Register Tagprefix=”asp” Namespace=”System.Web.UI” Assembly=”System.Web.Extensions, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35″
%>

<%@ Import Namespace=”Microsoft.SharePoint” %>

<%@Register Tagprefix=”WebPartPages” Namespace=”Microsoft.SharePoint.WebPartPages” Assembly=”Microsoft.SharePoint,
Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”
%>

<%@ Control Language=”C#” AutoEventWireup=”true” CodeBehind=”DelegateControl.ascx.cs” Inherits=”DelegateControls.ControlTemplates.SearchDelegateControls.DelegateControl”%>

<div>

<asp:TextBox ID=”SearchText” runat=”server” Width=”120″ Value=”Enter text in my custom search…”/>

<asp:Button Width=”30px” ID=”SearchButton” onclick=”SearchButton_Click” runat=”server” Text=”Search”/>

</div>

######

>After feature is installed, activate the feature

Activate feature

>The new custom form will be visible in the place of out of box search box

Custom Search box

>Now click deactivate feature

Deactivate Feature

>Out of box search box will be replaced.

Outofbox search box

Conclusion

Without touching master page or having script, we are still able to customize the masterpage controls with help of delegate controls.

This will be very helpful when we are using team sites where publishing is not enabled.

In further articles I will provide how to extend delegate controls which have datasource.

Hope you enjoy some good features of Sharepoint.

March 23, 2012 · Adi · 2 Comments
Tags:  · Posted in: Branding, Sharepoint 2007, Sharepoint 2010

2 Responses

  1. Avinash Dad - March 23, 2012

    Very nice. Without touching master page you are able to change look n feel.
    Its very independent; since you made it using feature activation/deactivation.

    Overall This is great article.
    Thanks for your post.

  2. adicodes - March 23, 2012

    You are welcome :)

Leave a Reply

What is 10 + 9 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)