Search This Blog

Wednesday, January 14, 2015

SharePoint Delegate Controls

SharePoint Delegate control is a mechanism that we can insert a code or control to the SharePoint Master page(and Page Layout) without touching any of code in the SharePoint Master page(and in Page Layout) and also it is a container type control which can holds child controls on it 


 SharePoint MasterPage has more Delegate Controls than those related to Navigation Controls and SiteMapProviders. In order to use this blog post as a quick reference I will list here all Delegate Controls available in v4.master with their main functionality.

Before I get into the list itself, I would like to add a couple of notes about delegate controls.


  1. Sequence numbers. These numbers determine which control will be loaded in a delegate control. A delegate control can only have one control, unless AllowMultipleControls is set to true (See the second note just below). The control with the lowest sequence number will be the one rendered in the page.
  2. The AllowMultipleControls gives the developer the option to load several controls one after the other. This means that a custom delegate control will not replace an existing one and the order will be determined by the sequence number.

After these two notes are clear, we can get an idea about delegate controls just here:

AdditionalPageHead


<SharePoint:DelegateControl runat="server" ControlId="AdditionalPageHead"AllowMultipleControls="true"/>

This delegate control allows the developer to add multiple html head lines. They will be rendered together with the ones SharePoint adds already there out of the box. For instance, we could load different .js libraries without modifying the existing MasterPage, or even build some logic behind our specific delegate control to load a JS library based on the logged user credentials.

GlobalNavigation


<SharePoint:DelegateControl runat="server" ControlId="GlobalNavigation"/>

This delegate control is used to add content at the top of the page, even on top of the “Site Actions” or “Welcome ID” Controls. SharePoint out of the box is using this delegate control to add some custom links, such as “My Network”, “My Content” and “My Profile” when we access our “My Site”.



GlobalSiteLink0


<SharePoint:DelegateControl runat="server" ID="GlobalDelegate0" ControlId="GlobalSiteLink0" />

This delegate control is used by SharePoint to place the Variations Menu. If we had multiple languages enabled in our site, we would be able to choose the appropriate language leveraged by the control “VariationsLabelMenu.ascx” placed within this delegate control.

GlobalSiteLink2


<SharePoint:DelegateControl ControlId="GlobalSiteLink2" ID="GlobalDelegate2" Scope="Farm"runat="server" />

SharePoint will use this delegate control to place a Social Delegate Control called “socialdata.ascx”. This control is the one in charge to render the links to “My Site” and “My Profile” when we expand the Welcome ID control.



PublishingConsole


<SharePoint:DelegateControl runat="server" ControlId="PublishingConsole"Id="PublishingConsoleDelegate">

This control is the one rendering the dynamic publishing console. We should call it Server Ribbon when we are referring to SharePoint 2010. This Server Ribbon is the component that enables the end-user, and content editors, to work with SharePoint as a full WYSIWYG CMS. It renders all buttons, tabs, contextual tabs and contextual tab groups that appear while working with pages, documents or SharePoint content in general. It is not necessary now, with SharePoint 2010, to tweak this control in order to add buttons and/or tabs, you can use some other supported way to add them to the Ribbon (it could be another topic to blog about in the future).



GlobalSiteLink3


<SharePoint:DelegateControl ControlId="GlobalSiteLink3" Scope="Farm" runat="server"/>

Renders two image links that are used to tag the current Page and open the SocialDataFrame.aspx page, allowing the user to manage the social tags and notes that are associated with the current Page.



SmallSearchInputBox


<SharePoint:DelegateControl runat="server" ControlId="SmallSearchInputBox" Version="4"/>

This delegate control renders the small search box. This search box control leverages the search functionality as it’s been configured in Site Collection Administration, such as whether to show the search scopes etc… you could build your own Search Input Box component and replace it wherever your feature is activated.



TopNavigationDataSource & QuickLaunchDataSource


<SharePoint:DelegateControl runat="server" ControlId="TopNavigationDataSource"Id="topNavigationDelegate">
<SharePoint:DelegateControl runat="server" ControlId="QuickLaunchDataSource">

I covered these two delegate controls with my post about custom Top and QuickLaunch menus. Just to give you an intro to this subject I could say that with these delegate controls we can override the datasources that feed the top menu and quicklaunch menu. Therefore you can customize the links that appear dynamically or show/hide links based on any business requirement.

TreeViewAndDataSource


<SharePoint:DelegateControl runat="server" ControlId="TreeViewAndDataSource">

This delegate control wraps together a DataSource and a TreeView control. This control is rendering the content provided by the datasource. So, as you can see, this is a pretty self-enclosed and neat solution. If you dive a bit deeper into the FEATURES folder within the 14 hive you will see this delegate control is used not only at MasterPage level but also in several application pages, mainly the ones giving you the option to pick up files, such as upload.aspx or Picture Library pages.

SharePoint 2013 Delegate Controls
And now that SharePoint 2013 is available, you can find what's been added to the Delegate Controls collection here... http://www.helpmeonsharepoint.com/2012/07/sharepoint-2013-new-delegate-controls.html
Scenario 1 (Creating Delegate Within Master Page Itself)

You can create new simple delegate within the master page itself.










Scenario 2 (Creating Custom delegate control using Visual Studio)
Steps:
1.       Open Visual Studio 2010 and create a new Empty SharePoint project.
2.       Map a new folder to the CONTROLTEMPLATES directory in the SharePoint root (the 14 hive).
3.       Add a new item visual web part and 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>




  • 4- Save the changes and deploy into the site.
  • 5- Upto now we created a delegate control and deployed. Now we want to add it to our site. 
  • 6-Now add the following code to our master page.

  • 1
    <SharePoint:DelegateControl ControlId="RegisterHereControl" AllowMultipleControls="true" runat="server"></SharePoint:DelegateControl>





    Importance:
    ·         Using the delegate control a developer can customize the SharePoint site controls without editing the
     code master page (After inserting the Delegate).
    ·         Suppose if you want add a counter for no.of.vistors to the site. We created a control for displaying 
    the total count 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 be changed knowingly or unknowingly. To avoid this we have delegate controls.




  • No comments:

    Post a Comment