SPNavigation - 显示用户定义的导航
我正在尝试在前端网站上构建辅助导航,但无法解决问题。在下面的代码中,输出显示类似以下内容:
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 />");
}
}
}
}
}
为什么上面的逻辑不适用于顶部节点下的子网站?我还没有完全理解“GlobalNodes”属性,所以我必须假设这就是问题所在。但我想不出解决办法。
任何帮助将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道这是很久以前的事了,但是代码中有一个错误,而且由于这对我来说在谷歌结果中的排名相当高,我认为我应该为遇到它的其他人纠正它。
SPNavigationNodeCollection SecondLevelWebs = FirstLevelWebChild.Navigation.GlobalNodes[0].Children;
应该是
SPNavigationNodeCollection SecondLevelWebs = FirstLevelWebChild.Children;
通过使用 FirstLevelWebChild.Navigation.GlobalNodes,您将再次获得根导航,而不是您想要获得的子导航。
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.