Partager via


SharePoint Navigation using Object Model

When we run SPWeb.CurrentNavigationNodes or PublishingWeb. CurrentNavigationNodes to get nodes collection we are getting it from database

But the publishing portal’s navigation does not enter newly created nodes into database unless we do some activity on the navigation provider via _layouts\AreaNavigationSettings.aspx.

I also tried to move/hide the nodes before fetching it using a similar console application ; Just to see if I get those newly added nodes. But I don’t and it’s because when I do SPNavigationNode.MoveToLast I am sorting or moving the database nodes and not the one which are there in Memory which will trigger entries of new nodes in database.

Workaround

The workaround to this issue is to use PortalSiteMapProvider class, get the provider that is being used and iterate, you will get all nodes, sorted the way you see in the Quick Launch

I have also attached this same code (.cs) in the attachment to this email as well.

The only issue here is that this code will only run in web context (httpcontext), so if you want to use in an ASPX page, you can create a user control or a webpart and add it in a page.

If you have a standalone tool which needs to access navigation nodes, then I suppose you will have to put this code in a webservice and consume it.

Here is the code

try

{

PortalSiteMapProvider sitemapprovider = (PortalSiteMapProvider)SiteMap.Providers["CurrentNavSiteMapProvider"];

SiteMapNode rootNode = sitemapprovider.CurrentNode;

writer.WriteLine(rootNode.Title + "<br>");

if (rootNode.ChildNodes.Count > 0)

{

foreach (SiteMapNode childNode in rootNode.ChildNodes)

{

writer.WriteLine(" - " + childNode.Title + "<br>");

}

}

}

catch (Exception e)

{

writer.WriteLine("Error: " + e.Message + System.Environment.NewLine + "Stack trace: " + e.StackTrace);

}