Export (0) Print
Expand All

How to: Customize the Display of the Top Link Bar

SharePoint 2010

Last modified: November 01, 2010

Applies to: SharePoint Foundation 2010

The top link bar that appears on most content pages in Microsoft SharePoint Foundation 2010 is a horizontal tabbed strip just below the website's title and description. The first tab is the "Home" position and usually has a link to the website's home page. Often other links on the bar point to websites lower in the site hierarchy.

The top link bar is rendered by an AspMenu control that is defined in the master page that creates the layout for pages in the website. In SharePoint Foundation 2010, the primary master page for both content pages and application pages is v4.master. You can change the appearance of the top link bar by modifying the markup in v4.master.

You can alter the appearance of the top link bar by setting properties of the AspMenu control that renders the navigation nodes for the bar. For a detailed discussion of the markup for the control and its data source, see Navigation Controls.

For example, you can set properties that add flyout menus for displaying subordinate items below links on the bar. This change can be useful for reducing clutter on a crowded top link bar because it enables you to use links on the static menu as categories and to move secondary links to flyout menus below the categories.

To add flyout menus to the top link bar

  1. In SharePoint Designer 2010, open the website that you want to customize.

  2. In the Navigation pane, select Master Pages.

  3. Right-click v4.master, and then click Copy.

  4. Right-click an empty area of the Master Pages pane, and then click Paste.

    The file v4_copy(1).master is created.

  5. Right-click v4_copy(1).master, click Rename, and then type a new name, such as my.master.

  6. Open the new file for editing.

  7. Locate the top link bar's menu control. You can find the control in code view by searching on the following string:

    ID="TopNavigationMenuV4"

  8. Set the value of the StaticDisplayLevels attribute and the MaximumDynamicDisplayLevels attribute to 1, as follows.

    <SharePoint:AspMenu
      ID="TopNavigationMenuV4"
      Runat="server"
      EnableViewState="false"
      DataSourceID="topSiteMap"
      AccessKey="<%$Resources:wss,navigation_accesskey%>"
      UseSimpleRendering="true"
      UseSeparateCss="false"
      Orientation="Horizontal"
      StaticDisplayLevels="1"MaximumDynamicDisplayLevels="1"
      SkipLinkText=""
      CssClass="s4-tn"/> 
    
  9. On the File menu, click Save.

    Note Note

    You might not be allowed to save changes to the site's default master page if a site-mapped page is open in the browser. If this occurs, close the browser and try again.

  10. In the Navigation pane, select Master Pages.

  11. Right-click the name of your master page, and then click Set as Default Master Page.

    Note Note

    You can also set a custom master page as the default master by writing code that sets the value of the SPWeb.MasterUrl property.

A flyout menu does not appear until one of the nodes on the top link bar contains child nodes. The following console application creates a new node with the display text "Resources" and adds one child node with the display text "SharePoint Developer Center."

using System;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb("parent"))
                {
                    if (!web.Navigation.UseShared)
                    {
                        string resTitle = "Resources";
                        string resUrl = web.Navigation.Home.Url;
                        string spdevTitle = "SharePoint Developer Center";
                        string spdevUrl = "http://msdn.microsoft.com/sharepoint";

                        // Get the top link bar.
                        SPNavigationNodeCollection topnav = web.Navigation.TopNavigationBar;

                        // If a Resources link exists, get it.
                        SPNavigationNode resLink = topnav
                            .Cast<SPNavigationNode>()
                            .FirstOrDefault(n => n.Title == resTitle);

                        // If the Resources link does not exist, create it.
                        if (resLink == null)
                        {
                            resLink = new SPNavigationNode(resTitle, resUrl);
                            resLink = topnav.AddAsLast(resLink);
                        }

                        // If the Resources node has a SharePoint Dev Center child, get it.
                        SPNavigationNode spdevLink = resLink
                            .Children
                            .Cast<SPNavigationNode>()
                            .FirstOrDefault(n => n.Url == spdevUrl);

                        // If the item does not exist, create it.
                        if (spdevLink == null)
                        {
                            spdevLink = new SPNavigationNode(spdevTitle, spdevUrl, true);
                            spdevLink = resLink.Children.AddAsLast(spdevLink);
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue....");
            Console.ReadLine();
        }
    }
}

The top link bar of a subsite can inherit the navigation links on the parent site's top link bar instead of defining its own set of links. A top link bar that is configured this way can provide a consistent experience for users who are moving between the parent site and the subsite. For more information, see How to: Share the Top Link Bar Between Sites.

However, you should be aware that configuring a subsite's top link to Use Links from Parent just means that the subsite uses the site map behind the parent's top link bar. The content of the top link bar is inherited. Its presentation is still determined by markup in the subsite's own default master page and in its own content pages. Although this fact may seem obvious, it is easy to overlook.

One way to achieve consistent presentation across all subsites is to set the MasterUrl property for the subsites so that it points to the parent site's master page. However, this approach could undo other local customizations and for that reason might not be desirable. A better approach might be to duplicate changes in the markup for the top link bar by making identical changes to each website's default master page.

Show:
© 2014 Microsoft