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)

Syntax

'Declaration
Public Sub New ( _
    title As String, _
    url As String, _
    isExternal As Boolean _
)
'Usage
Dim title As String
Dim url As String
Dim isExternal As Boolean

Dim instance As New SPNavigationNode(title, url, _
    isExternal)
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.

  • isExternal
    Type: System.Boolean

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

Remarks

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.

Examples

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();
        }
    }
}
Imports System
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Navigation

Module ConsoleApp

    Sub Main()

        Using site As New SPSite("http://localhost")

            Using web As SPWeb = site.OpenWeb("test")

                ' Get the Links list or create it if it does not exist.
                Dim list As SPList = web.Lists.TryGetList("Links")

                If list Is Nothing OrElse list.BaseTemplate <> SPListTemplateType.Links Then

                    Console.WriteLine("Creating links list.")

                    ' Create the list.
                    Dim listId As Guid = web.Lists.Add("Links", _
                        "A list of interesting Web pages.", _
                        SPListTemplateType.Links)
                    list = web.Lists.GetList(listId, False)
                End If

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

                ' Check if the list already has this link.
                Dim msdnItem As SPListItem = Nothing
                For Each item As SPListItem In list.Items
                    Dim rawValue As Object = item(SPBuiltInFieldId.URL)
                    Dim typedValue As New SPFieldUrlValue(rawValue.ToString())
                    If typedValue.Url = msdnValue.Url Then
                        msdnItem = item
                        Continue For
                    End If
                Next

                ' If it does not...
                If msdnItem Is Nothing Then

                    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()
                End If

                ' Get the QuickLaunch heading node for the links list.
                Dim linksNode As SPNavigationNode = web.Navigation.QuickLaunch.Cast(Of SPNavigationNode)().FirstOrDefault( _
                    Function(n) n.Title = list.Title)

                ' Create the heading if it does not exist.
                If linksNode Is Nothing Then
                    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)
                End If

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

                ' Create an external navigation node.
                Dim msdnNode As New SPNavigationNode(msdnValue.Description, msdnValue.Url, True)

                ' Add the MSDN node to QuickLaunch below the Links heading.
                msdnNode = linksNode.Children.AddAsFirst(msdnNode)

            End Using

        End Using

        Console.Write(vbCrLf & "Press ENTER to continue....")
        Console.Read()
    End Sub

End Module

See also

Reference

SPNavigationNode class

SPNavigationNode members

SPNavigationNode overload

Microsoft.SharePoint.Navigation namespace

SPNavigationNode.IsExternal