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开发者_C百科 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!
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
精彩评论