SPNavigation - 显示用户定义的导航

发布于 2024-08-05 07:04:55 字数 1451 浏览 0 评论 0原文

我正在尝试在前端网站上构建辅助导航,但无法解决问题。在下面的代码中,输出显示类似以下内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤带着心事 2024-08-12 07:04:55

我知道这是很久以前的事了,但是代码中有一个错误,而且由于这对我来说在谷歌结果中的排名相当高,我认为我应该为遇到它的其他人纠正它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文