Third Party HTML/JS

Wednesday, May 25, 2011

Create Custom Navigation for SharePoint:AspMenu using a asp:Repeater

As you are probably aware, there are many different ways to create a navigation in Sharepoint 2010. This blog will focus on ONE specific way that is both easy and powerful. I will show you how to create a left navigation without overwhelming readers with loads of information they don't need and can probably figure out on their own.

1) Create the menu hierarchy in site settings.
Go to Site Actions > Site Settings > Navigation
Create all the menu entries and sub-entries that you need for your menu.

2) In the master page, there should be a node for the SharePoint:AspMenu. It should look something like the following:

                    <!-- top navigation publishing data source -->
                  <PublishingNavigation:PortalSiteMapDataSource
                      ID="topSiteMap"
                      runat="server"
                      EnableViewState="false"
                      SiteMapProvider="GlobalNavigation"
                      StartFromCurrentNode="true"
                      StartingNodeOffset="0"
                      ShowStartingNode="false"
                      TrimNonCurrentTypes="Heading"/>
                   
                  <!-- top navigation menu (set to use the new Simple Rendering)
                  <SharePoint:AspMenu
                    ID="TopNavigationMenuV4"
                    EncodeTitle="false"
                    Runat="server"
                    EnableViewState="true"
                    DataSourceID="topSiteMap"
                    AccessKey="<%$Resources:wss,navigation_accesskey%>"
                    UseSimpleRendering="true"
                    UseSeparateCss="True"
                    Orientation="Vertical"
                    StaticDisplayLevels="2"
                    MaximumDynamicDisplayLevels="2"
                    SkipLinkText=""
                    CssClass="s4-tn">
                   </SharePoint:AspMenu>

It is not easy to apply custom styling to this and even the styling capabilities it has is limited.

3) In order to apply custom styling, we will use asp:Repeater to dynamically generate the desired html/css code. The following code can replace only the SharePoint:AspMenu and should allow you create whatever html/css code you need for styling. This is a two level navigation where each main header can have multiple children. This can be specified in the Navigation site settings.

                        <asp:Repeater runat="server" ID="MenuRepeater" DataSourceID="topSiteMap">
                           <HeaderTemplate><div class="arrowlistmenu"></HeaderTemplate>
                           <SeparatorTemplate><img src="images/eOne_leftmenu_divider.gif" height="2" width="164" /></SeparatorTemplate>
                           <ItemTemplate><h3 class="menuheader expandable"><a href="<%# Eval("Url")%>"><%# Eval("Title")%></a></h3>
                           <asp:Repeater runat="server" ID="ChildMenuRepeater" DataSource='<%# ((SiteMapNode)Container.DataItem).ChildNodes %>'>
                              <HeaderTemplate><ul class="categoryitems"></HeaderTemplate>
                              <ItemTemplate>
                              <li><a href="<%# Eval("Url")%>"><%# Eval("Title")%></a></li>
                              </ItemTemplate>
                              <FooterTemplate></ul></FooterTemplate>
                          </asp:Repeater>
                           </ItemTemplate>
                       
                           <FooterTemplate></div></FooterTemplate>
                       </asp:Repeater>


I will now explain some of the template elements in the asp:Repeater.

HeaderTemplate - Anything between these tags will be outputted ONCE before the repeater begins looping for the itemtemplate

SeparatorTemplate -Anything between these tags will be outputted between each item template

ItemTemplate - Anything between these tags will be applied for EACH element in the source data, in this case out navigation entries. Further more, you can use another repeater inside this to output the sub-entries as shown in the code above.

FooterTemplate - Anything between these tags will be outputted ONCE after the item templates have outputted.

I hope this tutorial have been helpful in creating a custom styled navigation for Sharepoint 2010.

Please contact for feedback or questions.

6 comments:

  1. When I tried to replace the ASPMenu with Repeater, I got the error: Code blocks are not allowed in this file. Any idea ?

    ReplyDelete
  2. To get this working you need to modify the web.config of your web application. I know I should have put this into the blog but you need to modify this tag: PageParserPaths.

    Follow this blog: http://blogs.msdn.com/b/kaevans/archive/2007/04/26/code-blocks-are-not-allowed-in-this-file-using-server-side-code-with-sharepoint.aspx

    ReplyDelete
  3. can we have the same modification done to Current Navigation instead of Top Link Bar

    ReplyDelete
  4. Thank you so much, the Repeater has always been my best friend so it is a blessing that I can use it to generate my menus in Sharepoint. To have better control I've added a method to handle the ItemDataBound of my repeater and there you go, I can do whatever I want with my menus!!

    ReplyDelete
  5. Any reason why following Your post here, loads a correct menu everywhere except for pages in the _layouts folder?

    And yes, I have set both the masterurl and custommasterurl. It loads the design except for the global navigation menu which is still the old aspmenu.

    ReplyDelete
  6. Hello!,
    This is good, but what about SharePoint 2013, Please give some details about its HTML Master page. this will be your kind.
    Thanks

    ReplyDelete