Export (0) Print
Expand All

How to: Share the Top Link Bar Between Sites

Last modified: October 05, 2010

Applies to: SharePoint Foundation 2010

In Microsoft SharePoint Foundation, you can provide users a consistent navigational experience across sites by specifying inheritance from the parent site, which is especially important when you need to provide navigation between peer subsites. In this case, each subsite created under the site shares a top link bar and is itself represented among the links. Without navigational inheritance, each site would have its own navigation. Shared navigation must be implemented through the top link bar of the parent site.

Sharing a top link bar across sites simplifies the user experience because it allows easier cross-site navigation. Shared navigation simplifies navigation, but it should not be a replacement for careful planning. Understand that the shared navigation control represents a "flat" site structure and could possibly confuse users about the actual hierarchy of sites created within a site collection.

You can implement a shared navigation control between sites within a site collection through the user interface. You can specify to share the top link bar either as you create each site or after you create each site.

To add the top link bar of the parent site to a subsite while creating the subsite

  1. On the Create page, click More Options.

  2. In the Navigation Inheritance section, after Use the top link bar from the parent site? click Yes.

  3. Click Create.

To add the top link bar of the parent site to an existing subsite

  1. Navigate to the subsite.

  2. Click Site Actions and then click Site Settings.

  3. On the Site Settings page, in the Look and Feel section click Top link bar.

  4. On the Top Link Bar page, click Use Links from Parent, and then click OK in the message box that opens.

Navigation operations on a website are controlled by settings stored on the SPNavigation object that is returned by the Navigation property of the SPWeb object that represents the site.

Whether or not a website uses the top link bar from its parent site depends on the setting of the UseShared property of the SPNavigation object. If UseShared returns true, the subsite inherits the top link bar from its parent site; otherwise, the subsite has its own top link bar, unique to the subsite.

Note Note

The root website in a site collection cannot inherit a top link bar because it has no parent website to inherit from. For this reason, SPWeb.Navigation.UseShared returns false whenever SPWeb.IsRootWeb returns true. An exception is thrown if you try to set the root website's UseShared property to true.

When you are creating a website in code, it is not possible to create the site and add the parent site's top link bar to the site in a single call. You must first create the website and then add the top link bar of the parent site. For an example, see the SPWebCollection.Add method.

To add the top link bar of the parent site to a subsite

  1. Get a reference to the SPWeb object that represents the subsite.

    Dim child As SPWeb = site.OpenWeb("parent/child")
    
  2. Verify that SPWeb.IsRootWeb returns false.

    If Not child.IsRootWeb Then
    
  3. Get the subsite's Navigation property and, on the object that is returned, set the UseShared property to true.

    child.Navigation.UseShared = True
    

Example

The following example is a console application that adds a parent website's top link bar to a subsite. The application also modifies the parent website's top link bar by adding a link to the subsite. The TopNavigationBar property of an SPNavigation object returns an SPNavigationNodeCollection object. Adding a node to the top link bar is simply a matter of creating an SPNavigationNode object and adding it to the SPNavigationNodeCollection that represents the top link bar.

Note that before adding a link, the application first checks to be sure the parent does not inherit links. If the parent does inherit links, its SPNavigationNodeCollection is null and thus a link cannot be added. If the collection is not null, the application checks whether a link to the child already exists on the parent's top link bar. If the parent has no existing link to the child, one is added.

Imports System
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Navigation

Module ConsoleApp

    Sub Main()

        Using site As New SPSite("http://localhost")

            Using child As SPWeb = site.OpenWeb("parent/child")

                ' Verify that this is not the root of a site collection.
                If Not child.IsRootWeb Then

                    ' Use links from parent on the child's top link bar.
                    child.Navigation.UseShared = True

                    ' If the parent web's top link bar is not inherited,
                    ' add a link to the child on the parent's top link bar.
                    If Not child.ParentWeb.Navigation.UseShared Then

                        ' Get the parent's top link bar.
                        Dim topnav As SPNavigationNodeCollection = child.ParentWeb.Navigation.TopNavigationBar

                        ' Query for an existing link to the child.
                        Dim node As SPNavigationNode = topnav.Cast(Of SPNavigationNode)().FirstOrDefault(Function(n) n.Url.Equals(child.ServerRelativeUrl))

                        ' No link, so add one.
                        If node Is Nothing Then
                            ' Truncate long a title.
                            Dim linkTitle As String = child.Title
                            If linkTitle.Length > 15 Then
                                linkTitle = linkTitle.Substring(0, 12) + "..."
                            End If

                            ' Create the node.
                            node = New SPNavigationNode(linkTitle, child.ServerRelativeUrl)

                            ' Add it.
                            node = topnav.AddAsLast(node)
                        End If

                    End If

                End If

            End Using

        End Using

        Console.Write(vbCrLf & "Press ENTER to continue....")
        Console.Read()
    End Sub

End Module
Show:
© 2014 Microsoft