Responsive Ads Here

Sunday, October 19, 2014

How to Implement custom delegate Control or over ride default delegate Control in SharePoint 2013

Delegate control is a control which gives a capability to replace or modify the information in master page.In SharePoint 2013 there are 3 new delegate controls has been added. 
  1. PromotedActions
  2. SuiteBarBrandingDelegate
  3. SuiteLinksDelegate    
As per the definition Delegate control gives us capability to replace or add the information in master page. Let us see how we can do the same with real time examples.
In my previous project i had a requirement as i need to replace the SharePoint text which is available on top left corner with the portal logo. So in this scenario generally we need to override the default delegate control. so we need to check what is the default delegate control which displays the SharePoint text.
SuiteBarBrandingDelagate is the name of the default delegate control.
Steps:--

1. Create an empty SharePoint Project
     
Project Selection
 
2. Add SharePoint mapped folder 
Mapped Folder Creation

 3. Select the control templates  mapped folder

Control Templates
 now the solution should be like this 

Control Templates
4. Right click on the control template folder, add a new item select user control and name it as what ever you want.

 5. I have added logo in to my images mapped folder. we can add either in mapped images folder or we can upload the logo in to style library.
6. write the below code snippet in the ascx file.       

Its just a div control with the class name and id. In code behind we can append the logo and navigation URL for the image from the code behind. 
7. Code snippet for the ascx.cs file

protected void Page_Load(object sender, EventArgs e)
{
   try
   {
     //Adding a literal control to the div
     BrandingTextControl.Controls.Add(new Literal
     {
         Text = string.Format("{2}",
                 SPContext.Current.Site.Url,
                    "/_layouts/15/images/samplelogo.png",
                    SPContext.Current.Site.RootWeb.Title)
                });

     }
     catch (Exception)
     {

        throw;
      }
}

8. Create an empty folder Delegate Controls and add an empty element. Now the solution will look like this.
Delegate Control

 9. Click on elements.xml file paste the below code snippet inside the elements tag.



Here id is the name of the delegate control what we want to over ride and sequence number as what ever you want. but here is one tricky thing we need to give the lesser sequence number then our customization will be loaded other wise default functionality will be loaded.
Final step is build and deploy then the SharePoint text will be over ride with the logo of the portal.

Below links give the complete idea for the same.
http://zimmergren.net/technical/sp-2013-some-new-delegatecontrol-additions-to-the-sharepoint-2013-master-pages
http://www.learningsharepoint.com/2013/02/22/change-sharepoint-in-top-left-bar-in-sharepoint-2013-with-logo/
I hope the above links give a complete idea about the delegate controls. 
Now we will see how to add the new functionality. I had a requirement to add the dynamic links in footer area of the master page. For custom delegate control every thing is same only the difference is we need to declare the custom delegate control in the master page.
In 2013 we know that we can upload an html mage automatically .master page file will be created. so declare the delegate control in the html page.
Example for the declaration is mentioned below.


Here we have declared the custom delegate control id as FooterNavigationDelegate. 
Same control id we need to mention in elements.xml file in empty element.
Find the below links which will give a complete idea.
http://mihirsharepoint.wordpress.com/2012/11/15/create-delegate-control-in-sharepoint/  

No comments:

Post a Comment