Delegate Controls in SharePoint 2013 using Visual Studio 2013

Create Delegate Controls in SharePoint 2013 using visual studio 2015

In this post, we will discuss delegate controls in SharePoint 2013. Through Delegate controls, you can inject a code or control to a SharePoint master page without touching the code of the master page.

This is how we can put a delegate control in the master page.

<SharePoint:DelegateControl runat=”server” AllowMultipleControls=”true” ControlId=”ControlID”/>

Also, read:
Record Management in SharePoint 2013 or SharePoint 2013 Online

Add Custom Web Part Properties in SharePoint 2013 Visual Web Part

How to send email from SharePoint Online using Rest API?

Example of few Delegate controls in SharePoint 2013 and SharePoint 2010:
SharePoint 2013:
SuiteLinksDelegate
SuiteBarBrandingDelegate
PromotedActions

SharePoint 2010:
AdditionalPageHead
GlobalNavigation
GlobalSiteLink3
GlobalDelegate2
GlobalDelegate0
TopNavigationDataSource
SmallSearchInputBox
QuickLaunchDataSource

Steps to Create a Delegate control using Visual Studio 2013 in SharePoint 2013:

In this step, we will discuss how we can create a simple delegate control using visual studio 2013. Open-File -> New Project, then select and Empty Project from the templates.

This it will ask to give your local SharePoint URL for debugging. Here choose the farm solution option also.

Delegate Controls in SharePoint 2013 using Visual Studio 2013
Delegate Controls in SharePoint 2013 using Visual Studio 2013

We will add a Mapped folder into the project. Right click on the project -> Add -> SharePoint Mapped Folder.. as shown below:

Delegate Controls in SharePoint 2013
Delegate Controls in SharePoint 2013

This will open the Add SharePoint Mapped Folder dialog box to select. There Select CONTROL TEMPLATES as shown in the fig below:

Delegate Controls in SharePoint 2016
Delegate Controls in SharePoint 2016

We will add one User Control inside the Control Templates folder. Right, click on the folder -> Add New Item and then select User Control (Farm Solution Only) which in under Office/SharePoint Visual C# templates. See fig below:

Create Delegate Controls in SharePoint 2013
Create Delegate Controls in SharePoint 2013

Here you can write the logic what you want to do inside the delegate control. I have just put a static message here.

Then our next step is to add an Empty Element to the project. For this Right click on the project -> Add New Item Then Select Empty Element from the list of templates as shown in the fig below:

Create Delegate Controls in SharePoint 2013 using visual studio 2015
Create Delegate Controls in SharePoint 2013 using visual studio 2015

Now your solution and project should look like below as shown in the fig below:

what are delegate controls in SharePoint 2013
what are delegate controls in SharePoint 2013

Now open your Elements.xml file and add the code like below:
<Control Id=”ControlToShowAlertMessage” Sequence=”1″ ControlSrc=”/_controltemplates/15/ShowAlertMessage.ascx” />

Here Id is the one Id we are giving (we will use this while giving reference in master page). And then ControlSrc is the path of the user control which we have created earlier.

delegate control sharepoint 2013 examples
delegate control SharePoint 2013 examples

Now you can build and deploy the project. Once deployed successfully, you will be able to see under the Site Feature if you have deployed in Web scope else if you have deployed in the site scope then it will appear in the site collection feature.

delegate control sharepoint 2016
delegate control SharePoint 2016

Now we can give the reference in the master page, Open your master page using SharePoint designer 2013. Then Check out and Edit in Advanced mode and then paste the below line in the master page.

<SharePoint:DelegateControl ControlId=”ControlToShowAlertMessage” AllowMultipleControls=”true” runat=”server”></SharePoint:DelegateControl>

Here ControlId is the id which we have given in the Elements.xml file.

Then check in the master page and publish the page as a major version, then your delegate control will appear like below:

delegate control sharepoint 2013
delegate control SharePoint 2013

Now let us say you do not need that to appear, then you can simply go to the Site Settings -> Manage Site Features. And then in the Manage Site Features page, you can Deactivate the feature. The delegate control will not appear.

Hope this will be helpful.


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →