Share via


SPNavigationNode Constructor (String, String)

Creates a new instance of the SPNavigationNode class that points to a location within the current site collection.

Namespace:  Microsoft.SharePoint.Navigation
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

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

Dim instance As New SPNavigationNode(title, url)
public SPNavigationNode(
    string title,
    string url
)

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

    A server-relative URL that points to content within the site collection.

Remarks

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 console application creates a link to a subsite on the Quick Launch menu for the parent 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("https://localhost"))
            {
                using (SPWeb web = site.OpenWeb("subsite"))
                {
                    // Make sure the Web site is not the root.
                    if (!web.IsRootWeb)
                    {
                        // Get the display text and URL for a link.
                        string title = web.Title;
                        string url = web.ServerRelativeUrl;

                        // Get the Sites heading on the parent's Quick Launch.
                        SPNavigationNode sitesHeading = web.ParentWeb.Navigation.GetNodeById((int)SPQuickLaunchHeading.Sites);
                        
                        SPNavigationNode webNode = null;
                        if (sitesHeading != null)
                        {
                            // Check for an existing link to the current Web site.
                            webNode = sitesHeading
                                .Children
                                .Cast<SPNavigationNode>()
                                .FirstOrDefault(n => n.Url == url);
                        }

                        // No link, so create one.
                        if (webNode == null)
                        {
                            // Create the node.
                            webNode = new SPNavigationNode(title, url);

                            // Add it to the parent web's quick launch.
                            webNode = web.ParentWeb.Navigation.AddToQuickLaunch(webNode, SPQuickLaunchHeading.Sites);
                        }

                        string format = "Quick Launch for {0} has a link to {1} at {2}";
                        Console.WriteLine(format, webNode.Navigation.Web.Title, webNode.Title, webNode.Url);
                    }
                }
            }
            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("https://localhost")

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

                ' Make sure the Web site is not the root.
                If Not web.IsRootWeb Then

                    ' Get the display text and URL for a link.
                    Dim title As String = web.Title
                    Dim url As String = web.ServerRelativeUrl

                    ' Get the Sites heading on the parent's Quick Launch.
                    Dim sitesHeading As SPNavigationNode = web.ParentWeb.Navigation.GetNodeById(CInt(SPQuickLaunchHeading.Sites))

                    Dim webNode As SPNavigationNode = Nothing
                    If sitesHeading IsNot Nothing Then
                        ' Check for an existing link to the current Web site.
                        webNode = sitesHeading.Children.Cast(Of SPNavigationNode)().FirstOrDefault(Function(n) n.Url = url)
                    End If

                    ' No link, so create one.
                    If webNode Is Nothing Then

                        ' Create the node.
                        webNode = New SPNavigationNode(title, url)

                        ' Add it to the parent web's quick launch.
                        webNode = web.ParentWeb.Navigation.AddToQuickLaunch(webNode, SPQuickLaunchHeading.Sites)
                    End If

                    Dim format As String = "Quick Launch for {0} has a link to {1} at {2}"
                    Console.WriteLine(format, webNode.Navigation.Web.Title, webNode.Title, webNode.Url)
                End If

            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