Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
SPNavigationNode constructor (String, String, Boolean)

SPNavigationNode constructor (String, String, Boolean)

Creates a new instance of the SPNavigationNode class and specifies the display name, locations, and specifies whether the new node is internal or external.

Namespace:  Microsoft.SharePoint.Navigation
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public SPNavigationNode(
	string title,
	string url,
	bool isExternal
)

Parameters

title
Type: System.String

Display name for the node. The string value can be a resource expression such as "$Resources:core,announceList", where "core" is the name of a resource file (.resx) and "announceList" is the name of a resource.

url
Type: System.String

URL of the node.

isExternal
Type: System.Boolean

true if the URL passed as the second argument points to an external location; otherwise false.

If the value of the isExternal parameter is false, the constructor attempts to create an internal node; if the constructor fails, it throws an exception.

The new SPNavigationNode object is not completely initialized until it has been added to a collection. For more information, see the SPNavigationNodeCollection class.

The following example is a console application that creates a Quick Launch heading for a list of links and adds a link to an external Web page as an item below the heading.

When the application creates the SPNavigationNode object for the heading, it calls the first (two-parameter) constructor because the target for the node is internal, a list on the current Web site. When it creates the link below the heading, the application calls the second (three-parameter) constructor because the target for the new node is a page on an external Web site.

using System;
using System.Linq;
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("test"))
                {
                    // Get the Links list or create it if it does not exist.
                    SPList list = web.Lists.TryGetList("Links");

                    if (list == null || list.BaseTemplate != SPListTemplateType.Links)
                    {
                         Console.WriteLine("Creating links list.");
                        
                        // Create the list.
                        Guid listId = web.Lists.Add("Links", 
                            "A list of interesting Web pages.", 
                            SPListTemplateType.Links);
                        list = web.Lists.GetList(listId, false);
                    }

                    // Create a link field value.
                    SPFieldUrlValue msdnValue = new SPFieldUrlValue();
                    msdnValue.Description = "SharePoint Developer Center";
                    msdnValue.Url = " http://msdn.microsoft.com/sharepoint";

                    // Check if the list already has this link.
                    SPListItem msdnItem = null;
                    foreach (SPListItem item in list.Items)
                    {
                        Object rawValue = item[SPBuiltInFieldId.URL];
                        SPFieldUrlValue typedValue = new SPFieldUrlValue(rawValue.ToString());
                        if (typedValue.Url == msdnValue.Url)
                        {
                            msdnItem = item;
                            continue;
                        }
                    }

                    // If it does not...
                    if (msdnItem == null)
                    {
                        Console.WriteLine("Adding a new link to the list."); 
                        
                        // Create a new list item and set the URL field value.
                        msdnItem = list.Items.Add();
                        msdnItem[SPBuiltInFieldId.URL] = msdnValue;
                        msdnItem.Update();
                    }

                    // Get the QuickLaunch heading node for the links list.
                    SPNavigationNode linksNode = web
                        .Navigation
                        .QuickLaunch
                        .Cast<SPNavigationNode>()
                        .FirstOrDefault(n => n.Title == list.Title);

                    // Create the heading if it does not exist.
                    if (linksNode == null)
                    {
                        Console.WriteLine("Creating a Quick Launch heading.");

                        // Create the node.
                        linksNode = new SPNavigationNode(list.Title, list.DefaultViewUrl);
 
                        // Add the node as a new QuickLaunch heading.
                        linksNode = web.Navigation.QuickLaunch.AddAsLast(linksNode);
                    }

                    Console.WriteLine("Adding a navigation node below the Quick Launch heading.");

                    // Create an external navigation node.
                    SPNavigationNode msdnNode = new SPNavigationNode(msdnValue.Description, msdnValue.Url, true);

                    // Add the MSDN node to QuickLaunch below the Links heading.
                    msdnNode = linksNode.Children.AddAsFirst(msdnNode);
                }
            }
            Console.Write("\nPress ENTER to continue....");
            Console.ReadLine();
        }
    }
}
Show:
© 2015 Microsoft