What is delegate control? Why to use it?

Delegate control allow developers to manage control on master page without touching or editing master page.

Actually delegate control is associated with a feature. Whenever that feature is active delegate control will be visible and if associated feature is de-active delegate control will not be visible to users.

Lets implement this by an example.
To do this, create a SharePoint project in Visual Studio 2010.
Add an user control and a module in your project.

Now your project hierarchy should look like this.

Write below line of code in UserControl1.ascx

 <h1>This is my Delegate Control Demo</h1> 

Now open Element.xml file under Module1.

Remove code between <Elements> ......</Elements> tags
So your new Elements.xml file should look like this.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control Id="mytest" Sequence="1000" ControlSrc="~/_controltemplates/DelegateControlDemo/UserControl1.ascx" />
 </Elements>

Now build and deploy your project.
Go to Site Settings -> Site Collection Administration -> Site Collection Features
Make sure your feature is activated.

Now go to your page and add your webpart (i.e. user control that you added)
Your output is shown in below screen.


Now again go to feature and de-activate this, come to your page and see the difference.
Hope you are now clear about delegate control.

Comments

Popular posts from this blog

CAML Query in SharePoint 2010

Calling REST APi from server side code - C#

All about SharePoint 2010 Content Type Hub

SharePoint Interview Questions and Answers

Calling ASP.Net WebMethod using jQuery AJAX