R – Custom SharePoint Navigation 2 Levels Deep

asp.netnavigationportalsitemapprovidersharepoint

I have a simple site consisting of a root site with 2 child sites.

Now I want the navigation for all 3 sites to be exactly the same and read:

Root Site | Child Site 1 | Child Site 2

With the currently selected site highlighted.

I have been able to nearly do this manually using an asp:repeater control and portalsitemapprovider. The problem is I can either get just the Root Site on the navigation (set ShowStartingNode to True) or just the child sites, I can't get both.

I know you can get both if you use a sharepoint:aspmenu control and set staticdisplaylevels to 2 but I don't like the messy code this control spits out. Basically i want to emulate this controls behaviour wrapped in my own compliant css. Any ideas?

  • Also as an addition does anyone have any ideas on how I can have it so if subsite1/2 have subsequent subsites/pages, it doesn't matter how many levels deep in the hierarchy a user is navigating, it still highlights the relevant subsite1 or subsite2? My current method compares currentnode with each of the providers nodes to work out which should be highlighted, but this doesn't work once the user has navigated away from the landing page of each subsite.

Thanks!

Best Answer

I think you have to start at the root and render that node in a first repeater and then use the childnodes of the root as a datasource for a second repeater, something like:

<asp:Repeater runat="server" ID="MenuRepeater" 
DataSourceID="MainNavigationDataSource">

<HeaderTemplate>
  // code here
</HeaderTemplate>

<ItemTemplate>
  // code here
<asp:Repeater runat="server" ID="ChildMenuRepeater"
DataSource='<%# ((SiteMapNode)Container.DataItem).ChildNodes %>'>

<HeaderTemplate>
  // code here
</HeaderTemplate>

<ItemTemplate>
  // code here

</ItemTemplate>

</ItemTemplate>

For your second question, I think (if I understand it correctly) that you could use SiteMapNode.IsDescendantOf() method but I somehow recall that that will not work in SharePoint so I have used this sort of code to check if a node is the "active" node:

string CurrentContextUrl = SPUtility.GetPageUrlPath(HttpContext.Current);
Uri CurrentUri = new Uri(CurrentContextUrl);
bool Active = CurrentUri.LocalPath.Equals(currentNode.Url);

Hope it helps out somehow :-)

Related Topic