SPNavigation.AddToQuickLaunch Method

Adds a node to the Quick Launch under the specified heading. If the specified heading does not exist, it is created.

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

Syntax

'Declaration
Public Function AddToQuickLaunch ( _
    node As SPNavigationNode, _
    heading As SPQuickLaunchHeading _
) As SPNavigationNode
'Usage
Dim instance As SPNavigation
Dim node As SPNavigationNode
Dim heading As SPQuickLaunchHeading
Dim returnValue As SPNavigationNode

returnValue = instance.AddToQuickLaunch(node, _
    heading)
public SPNavigationNode AddToQuickLaunch(
    SPNavigationNode node,
    SPQuickLaunchHeading heading
)

Parameters

Return Value

Type: Microsoft.SharePoint.Navigation.SPNavigationNode
Returns the SPNavigationNode object that was added.

Examples

The following console application adds a link to the "Links" list under the "Lists" heading in Quick Launch.

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())
                {
                    // 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)
                    {
                        // Create the list.
                        Guid listId = web.Lists.Add("Links", "Interesting hyperlinks", SPListTemplateType.Links);
                        list = web.Lists.GetList(listId, false);
                    }

                    // Check for an existing link to the list.
                    SPNavigationNode listNode = web.Navigation.GetNodeByUrl(list.DefaultViewUrl);
 
                    // No link, so create one.
                    if (listNode == null)
                    {
                        // Create the node.
                        listNode = new SPNavigationNode(list.Title, list.DefaultViewUrl);

                        // Add it to Quick Launch.
                        listNode = web.Navigation.AddToQuickLaunch(listNode, SPQuickLaunchHeading.Lists);
                    }
                }
            }
            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()

                ' 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
                    ' Create the list.
                    Dim listId As Guid = web.Lists.Add("Links", "Interesting hyperlinks", SPListTemplateType.Links)
                    list = web.Lists.GetList(listId, False)
                End If

                ' Check for an existing link to the list.
                Dim listNode As SPNavigationNode = web.Navigation.GetNodeByUrl(list.DefaultViewUrl)

                ' No link, so create one.
                If listNode Is Nothing Then
                    ' Create the node.
                    listNode = New SPNavigationNode(list.Title, list.DefaultViewUrl)

                    ' Add it to Quick Launch.
                    listNode = web.Navigation.AddToQuickLaunch(listNode, SPQuickLaunchHeading.Lists)
                End If

            End Using

        End Using

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

End Module

See Also

Reference

SPNavigation Class

SPNavigation Members

Microsoft.SharePoint.Navigation Namespace