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.