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”/>
Example of few Delegate controls in SharePoint 2013 and SharePoint 2010:
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.
We will add a Mapped folder into the project. Right click on the project -> Add -> SharePoint Mapped Folder.. as shown below:
This will open the Add SharePoint Mapped Folder dialog box to select. There Select CONTROL TEMPLATES as shown in the fig below:
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:
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:
Now your solution and project should look like below as shown in the fig below:
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.
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.
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:
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 Tutorials: