This topic has not yet been rated Rate this topic

SPNavigationNode Class

Represents a node in a hierarchical navigation structure for a Microsoft SharePoint Foundation Web site.

System.Object
  Microsoft.SharePoint.Navigation.SPNavigationNode

Namespace:  Microsoft.SharePoint.Navigation
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
[SubsetCallableTypeAttribute]
public sealed class SPNavigationNode

Unlike the SPNavigationNodeCollection class, the SPNavigationNode class requires you to call the Update method to cause property changes to existing nodes to be saved to the database. However moves occur immediately.

The following console application prints a simple map of the navigation structure for a Web site.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string format = "|-{0} (Id = {1})";

                    foreach (SPNavigationNode globalNode in web.Navigation.GlobalNodes)
                    {
                        Console.WriteLine(format, globalNode.Title, globalNode.Id);

                        foreach (SPNavigationNode childNode in globalNode.Children)
                        {
                            Console.WriteLine("  " + format, childNode.Title, childNode.Id);
                        }
                        Console.WriteLine("|");
                    }
                }
            }
            Console.Write("\nPress ENTER to continue....");
            Console.ReadLine();
        }
    }
}
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
SPNavigationNode Example

While having representation throughout SharePoint, an example of using the SPNavigationNode class is present when building up collections of nodes through properties like SPWeb.Navigation.QuickLaunch.

An example of this is below:

public static bool DeleteExampleLinkFromQuickLaunch(SPWeb web)
{
var success = false;
try
{
foreach (SPNavigationNode quickLaunchNode in web.Navigation.QuickLaunch)
{
for (var index = quickLaunchNode.Children.Count - 1; index >= 0; index--)
{
quickLaunchNode.Children[index].Delete();
}
}
success = true;
}
catch (Exception ex)
{

}
return success;
}

Adam Buenz
SharePoint Foundation MVP - http://www.sharepointsecurity.com