One thing that bugs me (and a lot of clients of us) is that SharePoint's top menu (AKA Global navigation) highlighting is not working as we expect it to:
If you create a subsite directly underneath the root web, and make it appear on global navigation, It will get highlighted when selected. BUT if you create a subsite somewhere else (say in the Site Directory), and then make it appear on global navigation, it will not get highlighted when you navigate to it.
If you create a publishing page it gets highlighted when visited. BUT if you create a page in a document library it doesn't.
Recently one of our clients asked me to fix this. So in this post we will see how to customize the built-in SharePoint's menu control to enhance the support for highlighting.
* If you are not interested in the how to, you can simply download the final product from here. Deployment instructions provided below.
While developing SharePoint, the product team has decided to use asp.net's menu control for navigation. But, it had a few issues, so they had created a new menu, that inherits from asp.net's menu. The new SharePoint menu class was marked as sealed, and this is bad for any of us who wish to customize it. Luckily, the product team has provided us with the complete source code for the SharePoint menu so we can make modifications to it. More info on this here.
We need to know when to highlight items. I chose to do that by comparing the URL that the menu item redirects to, to the URL of the page we are currently in. if they are identical, this means we are in the site that this menu item refers to, and it should be highlighted.
We will add our code to the OnMenuItemBound event. this way we have access to each menu item through the event's arguments.
add the following code to the OnMenuItemDataBound event:
if (SPContext.Current.ListItemServerRelativeUrl.ToLower() == MakeServerRelative(e.Item.NavigateUrl.ToLower()))
e.Item.Selected = true;
This code uses another method called MakeServerRelative. so add the following code to you class:
private string MakeServerRelative(string url)
//checks if the path is absolute
url = url.Remove(0, 7);
url = url.Remove(0, url.IndexOf('/'));
Don't forget to sign the assembly.
That's it! build the project and you are done.
This is his comment:
"Nicholas Hadlee said: Hi, I found a strange situation where this can cause an nullreference exception in some pages. The seachResults.aspx was causing it for me. I added an <code>if (SPContext.Current.ListItemServerRelativeUrl != null )</code> whcih seemed to fix things up. Good job getting this to work anyway."
You could create a MOSS solution for that, but I am too lazy 🙂 so what you need to do here is:
1. Install the assembly to the GAC.
2. Add SafeControl registration to web.config.
As the nature of menus, we will want them in our master page:
1. Edit your master page and add the registration similar to this one at the beginning of the page:
<%@ Register Tagprefix="Itaysk" Namespace="Itaysk.SharePoint.Controls" Assembly="EnhancedSPMenu, Version=188.8.131.52, Culture=neutral, PublicKeyToken=ff4fcc0bc7f78f41" %>
(If you use my attached assembly then copy and pate this line. Oterwise the namespace and assembly information may differ from your build)
2. Paste the following markup to some place holder, usualy PlaceHolderHorizontalNav place holder.
*This will create another menu in addition to the old one. You can delete the old one which is in the "PlaceHolderHorizontalNav" place holder
**You can also replace the existing <SharePoint:AspMenu tag with <Itaysk:MossMenu. Don't forget the closing tag – </SharePoint:AspMenu> turns to </Itaysk:MossMenu>
This is the markup for the menu control (and it's data source):
<StaticMenuItemStyle CssClass="ms-topnav" ItemSpacing="0px"/>
<StaticSelectedStyle CssClass="ms-topnavselected" />
<StaticHoverStyle CssClass="ms-topNavHover" />
<DynamicMenuStyle BackColor="#F2F3F4" BorderColor="#A7B4CE" BorderWidth="1px"/>
<SharePoint:DelegateControl runat="server" ControlId="TopNavigationDataSource">