Wednesday, January 29, 2014

Create delegate control in sharepoint

Hello,
Today I am going to explain about creating delegate controls in sharepoint.
If you want to see any delegate controls you can visit in the master page. For publishing site there is nightandday.master page which is default master page for the home page of the site.
You can find the following delegate controls in the nightandday.master page.
1
2
<SharePoint:DelegateControl runat="server" ControlId="AdditionalPageHead" AllowMultipleControls="true"/>
<SharePoint:DelegateControl ControlId="GlobalSiteLink3-mini" Scope="Farm" runat="server"/>
Before moving to how to create a delegate control, first we look at what is the importance of delegate control.
Importance:
  • Using the delegate control a developer can customize the SharePoint site controls without editing or even touching the master page.
  • Suppose we created a control for displaying the advertisements in the master page. If we want to keep this control in the master page we need to edit that master page and add all the controls over here. At any time if any mistake happen then whole master page will disturb. To avoid this we have delegate controls.
  • Mapping the delegate control to master page
    After creating the delegate control now we want to add it to the master page. To do this we have one tag that is
    1
    <SharePoint:DelegateControl runat="server" ControlId="" AllowMultipleControls=""/>
    ControlId –> is the Id of the control which we given in the elements.xml file while deploying the file. (Don’t worry at this time. We will cover in how to create delegate control in Visual Studio section)
    This is the main element for delegate control.
    Creating delegate control in Visual Studio
    Now I am creating a small control to enable a link for registering the new user in home page.
    To do this, you have to enable the forms based authentication. If you are not familiar to how to setup forms based authentication for sharepoint 2010 site then visit previous post.
  • After setting up the forms based authentication, open the visual studio and create an empty project.
  • Now add the control templates mapped folder, visual webpart, empty element like below image.

  • Now add the following code to the designer surface of the visual webpart
  • 1
    2
    3
    <div id="RegisterUserDiv" runat="server">
     <a href="#">Sample Delegate Control</a>
    </div>
  • In elements.xml file under the empty element you added, add the following code.
  • 1
    2
    3
    4
    <?xml version="1.0" encoding="utf-8"?>
      <Control Id="RegisterHereControl" Sequence="1000" ControlSrc="~/_controltemplates/RegisterUser.ascx" />
    </Elements>
  • Save the changes and deploy into the site.
  • Upto now we created a delegate control and deployed. Now we want to add it to our site. Now add the following code to our master page.
  • 1
    <SharePoint:DelegateControl ControlId="RegisterHereControl" AllowMultipleControls="true" runat="server"></SharePoint:DelegateControl>
  • It will look like this.
  • No comments:

    Post a Comment