SharePoint static 2nd level current navigation in MOSS 2007

Summary

Requirements:

  • SharePoint MOSS 2007
  • Must be using a Publishing Site

This post will create the SharePoint Current Navigation control that only shows the 2nd level site when browsing, and supports highlighting. The datasource comes from SharePoint as well, so everything is security trimmed, and editable in the browser.

This post is for users who are using SharePoint MOSS 2007 navigation on their website. If you want your current navigation to display the sites and pages under a subsite, but always keep the second level subsite on top, then this post is for you. It’s hard to explain, so reference the screenshot below:

Figure 1 – Our Firm is a subsite under the root of the website. Even if I navigate to Board of Directors above, Our Firm remains at the top of the Current Navigation.

Sitemap data for my screenshot above:

  • Company
    • Our Firm
      • Leadership
        • Management Team
        • Board of Directors
      • Contact Us
      • Locations
    • Investment Banking
    • Sales & Training
    • Research

Sitemap levels:

  • Top Level (root)
    • Second Level site
      • Third Level Site
        • Fourth Level site
        • Fourth Level site
      • Third Level Site
      • Third Level Site
    • Second Level site
    • Second Level site
    • Second Level site

The key is that I want only each second level navigation block to appear when a user is anywhere on or under that second level site. So in my screenshot, you see Our Firm, even if you navigate to Board of Directors.

This navigation also supports highlighting and pages.

Preparing the subsite (our Firm)

  • Check Show Subsites and Show Pages
  • Under Current Navigation, select Display current, below, and siblings. (the middle radio button)

Now you need to adjust your current navigation code in the MASTERPAGE to only show the site you are under.

Adjusting the masterpage Current Navigation

Find your PlaceHolderLeftNavBar and replace the contents with this, or you can just mimic it. Take note of the highlighted parts. These are important for this post.

Placeholders are content buckets in your masterpages, the content inside them is what is important. Just make sure you don’t have multiple of the same ID’s on the same masterpage.

Here is the code:

<asp:ContentPlaceHolder id=”PlaceHolderLeftNavBar” runat=”server”>

    <!– Current Navigation –>

    <!– Current Navigation –>


    <SharePoint:AspMenu ID=”CurrentNav” runat=”server” datasourceID=”SiteMapDS” orientation=”Vertical”

    StaticDisplayLevels=”4″ MaximumDynamicDisplayLevels=”1″ StaticSubMenuIndent=”1″ ItemWrap=”true”

    AccessKey=”3″ CssClass=”leftNav” SkipLinkText=”<%$Resources:cms,masterpages_skiplinktext%>”>

        <StaticHoverStyle CssClass=”LeftStaticHover” Font-Bold=”False” Font-Italic=”False” Font-Overline=”False” Font-Strikeout=”False” Font-Underline=”False” />

        <StaticSelectedStyle CssClass=”LeftStaticSelected” Font-Bold=”False” Font-Italic=”False” Font-Overline=”False” Font-Strikeout=”False” Font-Underline=”False” />

        <DynamicMenuStyle CssClass=”LeftDynamicMenu” Font-Bold=”False” Font-Italic=”False” Font-Overline=”False” Font-Strikeout=”False” Font-Underline=”False” />

        <DynamicMenuItemStyle CssClass=”LeftDynamicMenuItem” Font-Bold=”False” Font-Italic=”False” Font-Overline=”False” Font-Strikeout=”False” Font-Underline=”False” />

            <LevelMenuItemStyles>

                <asp:menuitemstyle CssClass=”parent” Font-Underline=”False” />

                <asp:menuitemstyle CssClass=”child” Font-Underline=”False” />

                <asp:menuitemstyle CssClass=”leftNav3″ Font-Underline=”False” />

                <asp:menuitemstyle CssClass=”leftNav4″ Font-Underline=”False” />

                <asp:menuitemstyle CssClass=”leftNav5″ Font-Underline=”False” />

            </LevelMenuItemStyles>

        <StaticMenuStyle CssClass=”LeftStaticMenu” Font-Bold=”False” Font-Italic=”False” Font-Overline=”False” Font-Strikeout=”False” Font-Underline=”False” />

        <DynamicSelectedStyle CssClass=”LeftDynamicSelected” Font-Bold=”False” Font-Italic=”False” Font-Overline=”False” Font-Strikeout=”False” Font-Underline=”False” />

        <StaticMenuItemStyle CssClass=”LeftStaticMenuItem” Font-Bold=”False” Font-Italic=”False” Font-Overline=”False” Font-Strikeout=”False” Font-Underline=”False” />

        <DynamicHoverStyle CssClass=”LeftDynamicHover” Font-Bold=”False” Font-Italic=”False” Font-Overline=”False” Font-Strikeout=”False” Font-Underline=”False”/>

    </SharePoint:AspMenu>

    <PublishingNavigation:PortalSiteMapDataSource ID=”SiteMapDS” Runat=”server”

    SiteMapProvider=”CombinedNavSiteMapProvider

    StartFromCurrentNode=”True”

    StartingNodeOffset=”1″ TrimNonCurrentTypes=”Page”/>

</asp:ContentPlaceHolder>

Here is an explanation of the above code:

SharePoint:AspMenu Creates an ASP.Net menu (html) using tables
datasourceID This sets the Datasource to populate the HTML. In this case, it points to the ID of the PublishingNavigation:PortalSiteMapDataSource.
StaticDisplayLevels How many levels to display (non flyouts) starting from the data returned. If your datasource only returns second level sites, the ASP Menu sees these as the first level.
MaximumDynamicDisplayLevels 1 disables flyouts. 2 would have the 5th level links as a flyout.

Datasource properties

PublishingNavigation:PortalSiteMapDataSource Creates a datasource on our page. This control uses the SharePoint Navigation.
ID Sets the ID of this datasource for ASP.Menu or other controls to access the data
SiteMapProvider There are a lot. I really like CombinedNavSiteMapProvider for this blog post. It was the best I found. Here are a few more
StartFromCurrnetNode Not sure why, but I think this controls the highlighting of sub sites. If this is set to false, I think the returned data source does not know what site you are located in. More here
StartingNodeOffset This is the reason why we see the second level page at the top of our left navigation, and not the root. We trim the root, as 0 is default, but we specify 1. This trims one level off our data source. See a full explanation here
TrimNonCurrnetTypes Since we specify pages, this removes all pages from the datasource that are not under the current site, and not below. See here for more info

See this same table (and ALL OTHER PROPERTIES!) here: http://msdn.microsoft.com/en-us/library/bb897657.aspx

Basically, just play around with the code. You can learn a lot just modifying the above properties. I basically wanted this blog post to show a neat configuration of SharePoint navigation 🙂

Good luck!