SPNavigation.Children 始终返回 0
我正在与 SP2010 基金会合作,但我认为自 WSS3 以来没有太大变化。
我正在尝试为基础项目创建一个自定义菜单,这是我迄今为止所拥有的。
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://localhost:3002/"))
{
SPNavigation nav =site.RootWeb.Navigation;
//gets the correct order of top level menu items
SPNavigationNodeCollection nodes = nav.TopNavigationBar;
using (SPWeb web = site.OpenWeb())
{
SPNavigationNode toplinkbar = web.Navigation.GetNodeById(1002);
if (toplinkbar != null)
{
foreach (SPNavigationNode node in toplinkbar.Children)
PrintNode(node);
}
}
}
Console.Write("\nPress ENTER to continue....");
Console.ReadLine();
}
static void PrintNode(SPNavigationNode node)
{
Console.WriteLine(node.Title);
foreach (SPNavigationNode item in node.Children)
PrintNode(item);
}
因此,第一部分 toplinkbar.Children 工作正常,但是当我知道站点内有子站点时,PrintNode node.Children 中的 foreach 循环始终返回 0。
我是否打算使用 SPSiteMapProvider 获取子站点?
任何帮助都会很棒,因为这让我发疯!
I am working with SP2010 foundation but I dont think much has changed since WSS3.
I am trying to create a custom menu for a foundation project this is what I have so far.
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://localhost:3002/"))
{
SPNavigation nav =site.RootWeb.Navigation;
//gets the correct order of top level menu items
SPNavigationNodeCollection nodes = nav.TopNavigationBar;
using (SPWeb web = site.OpenWeb())
{
SPNavigationNode toplinkbar = web.Navigation.GetNodeById(1002);
if (toplinkbar != null)
{
foreach (SPNavigationNode node in toplinkbar.Children)
PrintNode(node);
}
}
}
Console.Write("\nPress ENTER to continue....");
Console.ReadLine();
}
static void PrintNode(SPNavigationNode node)
{
Console.WriteLine(node.Title);
foreach (SPNavigationNode item in node.Children)
PrintNode(item);
}
So the 1st part toplinkbar.Children is working correctly however the foreach loop in PrintNode node.Children is always returning 0 when I know there child sites within the site.
Am I meant to be getting the subsites using SPSiteMapProvider?
Any help would be great as this is driving me nuts!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用附加到 TopNavigation 的 NavigationNodeCollection。这是 TopNav 信息的规范来源。
当您以这种方式加载“根”节点时,它会以断开连接的方式加载。
我有一篇旧博客文章可能会有所帮助。 http://gavinb.net/2008 /05/27/sharepoint-navigation-elements-如何使用它们进行编码
Just use the NavigationNodeCollection attached to the TopNavigation. That is cannonical source of TopNav information.
When you load that 'Root' node in that manner it's loaded in a disconnected manner.
I have an old blog post that might help somewhat. http://gavinb.net/2008/05/27/sharepoint-navigation-elements-how-to-code-with-them