What is delegate control in SharePoint? Why to use it?

Delegate control in SharePoint allows developers to manage control on a 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 an associated feature is de-active delegate control will not be visible to users.

Create Delegate control in SharePoint

Let’s implement this by an example.
To do this, create a SharePoint project in Visual Studio 2010.
Add a 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.

You may like these blogs –

Leave a Comment

RSS
YouTube
YouTube
Instagram