Create Top Navigation for SharePoint Custom Master Page





As the part of customization masterpage sometimes you need to add top level navigation with specified sites. Here is the coolest way to achieve this using aspx menu bar control.

Create a visual webpart in visual studio 2010 and add aspx menu control on the user control.

<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" CssClass="xxxxx"><asp:Menu>
// Apply styles as your wish

Take a custom list and create column [ ex:MenuUrl]  that using for url of the top navigations[Ex: https://xxxxxxx/home.aspx]. And add this list content to aspx menu on page load event as shown in below code.

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["ListMenu"];
SPListItemCollection items = list.Items;
// get the list item for menu control
//Title is the display name and MenuUrl refers the navigation url.
foreach (SPListItem item in items)
{
Menu1.Items.Add(new MenuItem(item["Title"].ToString(), item["Title"].ToString(), null, item["MenuUrl"].ToString().Split(',')[0]));
}

   Menu1.DataBind();
  }
}

add register this user control in your custom masterpage
<%@ Register TagPrefix="menu" TagName="menuweb" src="~/_controltemplates/xxxxx.ascx" %>

and call this in your cusomt master page specified location.

<menu:menuweb runat="server" id="menunew"/>

Is working like charm. If your remove some the tabs in top navigation simply go to custom list as we created for top navigation and delete the item.

Comments

Popular posts from this blog

PAGE NOT FOUND ERROR PAGE IN SHAREPOINT 2013

Send Mails in SharePoint Event Receiver

Get Host site list items in SharePoint Hosted App.