Create a Multilevel Hierarchical Menu in SharePoint 2013 with SuiteBar Branding Delegate Control

Ashok Raja
 
Solutions Architect
April 10, 2013
 
Rate this article
 
Views
3923

This post shows you how to create a multilevel navigation menu by overriding the default SharePoint 2013 Suit Bar Branding delegate control. By default the suit bar control displays the text “SharePoint” at the top left corner of the page. Now we can see how to utilize that space to display a custom hierarchical navigation menu.

Menu

Menu Links

The menu items are created based on the data available in a SharePoint List. For this demo I have created a list named as “Top Menu” at the root site of the site collection. The below is the structure of “Top Menu” list.

List Schema

Note : The column “ParentLink” is a lookup column referencing the “Title” column of the same list. “DisplayOrder” field is used to arrange the menu items based on the value provided in this column.

I have added data to the list as shown below.

List Items

Steps Involved

Since I have provided the full source code of this menu control for download at the bottom of the post,  I’ll explain it in a higher level.

1. Create a new SharePoint 2013 empty project in Visual Studio 2013

2. Create a new mapped folder to control templates and add a new user control named as “HierarchicalNav.ascx

3. Add an Empty Element File to the project and add the below code , that maps the user control that we have added in the previous step to suitbarbrandingdelegatecontrol , to the element file

 <?xml version="1.0" encoding="utf-8"?>
 <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   <Control Id="SuiteBarBrandingDelegate" Sequence="10" ControlSrc="~/_controltemplates/15/SFS.Navigation/HierarchicalNav.ascx" xmlns="http://schemas.microsoft.com/sharepoint/" />
 </Elements>

4. Add a literal control to the user control so that the menu can be rendered and flushed to the literal control.

5. Add CSS to the user control to transform html “li” elements to menu item (available with source code)

6. Implement the menu rendering functionality by retrieving the listitems from the SharePoint List

The below shown code is the core “recursive” function which builds the hierarchical menu based on the SharePoint list items

 private void RenderItems(StringBuilder sb, List<MenuItem> AllItems, MenuItem Node)
 {
     var Items = AllItems.Where(h => h.Parent == Node.Title).OrderBy(q => q.DisplayOrder);
     if (Items != null && Items.Count() > 0)
     {
         sb.AppendLine("<li>");
         sb.AppendLine(@"<a href=""" + Node.URL + @""">" + Node.Title + "</a>");
         sb.AppendLine("<ul>");
         foreach (var item in Items)
         {
             if (!HasChildItems(AllItems, item.Title))
                 sb.AppendLine(@" <li><a href=""" + item.URL + @""">" + item.Title + "</a></li>");
             RenderItems(sb, AllItems, item);
         }
         sb.AppendLine("</ul>");
         sb.AppendLine("</li>");
     }
 }

Download Source Code

Author Info

Ashok Raja
 
Solutions Architect
 
Rate this article
 
I am Ashok Raja, Share Point Consultant and Architect based out of Chennai, India. ...read more
 

Leave a comment