views:

381

answers:

1

I'm trying to build a secondary navigation on a front end site, and I can't get past an issue. In the code below, the output displays something similar to:

water
-----water
-----pollination
-----dormancy
pollination
-----water
-----pollination
-----dormancy
dormancy
-----water
-----pollination
-----dormancy
// Loop through Root Webs in Site Collection
foreach (SPWeb TopLevelWebs in CurrentSite.AllWebs)
{
    SPNavigationNodeCollection FirstLevelWebs = TopLevelWebs.Navigation.GlobalNodes[0].Children;

    // Loop through each Child of Web
    foreach (SPNavigationNode FirstLevelWebChild in FirstLevelWebs)
    {
        // Skip over pages and only look at webs (or "Area" as it is called in 'Properties')
        if (FirstLevelWebChild.Properties["NodeType"].ToString() != "Page")
        {
            Response.Write(FirstLevelWebChild.Title + "<br />");

            SPNavigationNodeCollection SecondLevelWebs = FirstLevelWebChild.Navigation.GlobalNodes[0].Children;
            foreach (SPNavigationNode SecondLevelWebChild in SecondLevelWebs)
            {
                // Skip over pages and only look at webs (or "Area" as it is called in 'Properties')
                if (SecondLevelWebChild.Properties["NodeType"].ToString() != "Page")
                {
                    Response.Write("-----" + SecondLevelWebChild.Title + "<br />");
                }
            }
        }
    }
}

Why will the logic above not work for subsites under the top nodes? I haven't fully understood the "GlobalNodes" property, so I have to assume that is where the problem lies. But I can't come up with a solution.

Any help would be greatly appreciated.

A: 

I know this is from quite a while ago, but there's a bug in the code, and since this came up pretty high on the google results for me I figure I should correct it for anyone else who comes across it.

SPNavigationNodeCollection SecondLevelWebs = FirstLevelWebChild.Navigation.GlobalNodes[0].Children;

Should be

SPNavigationNodeCollection SecondLevelWebs = FirstLevelWebChild.Children;

By using FirstLevelWebChild.Navigation.GlobalNodes you are again getting the root navigation instead of the sub-navigation that you're intending to get.

James