SPNavigationNodeCollection.AddAsLast method

Adds an SPNavigationNode object as the last node in the collection.

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

public SPNavigationNode AddAsLast(
	SPNavigationNode node


Type: Microsoft.SharePoint.Navigation.SPNavigationNode

Specifies the node object (SPNavigationNode) to add to the collection.

Return value

Type: Microsoft.SharePoint.Navigation.SPNavigationNode
The navigation node that was added, now completely initialized.

An SPNavigationNode object is not completely initialized until it has been added to a collection. If the object is not yet a member of a collection, read-only properties such as Id and ParentId return a null reference (Nothing in Visual Basic).

The following example is a console application that adds a link to a child Web site on the parent Web site's top link bar. Note that before adding the link, the application first checks to be sure the parent does not inherit links. If the parent does inherit links, its SPNavigationNodeCollection is null and thus a link cannot be added. The application also checks whether a link to the child already exists on the parent's top link bar. If the parent does not inherit links and has no existing link to the child, one is added.

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 child = site.OpenWeb("parent/child"))
                    // Use links from parent on the child's top link bar.
                    child.Navigation.UseShared = true;

                    // If the parent web's top link bar is not inherited,
                    // add a link to the child on the parent's top link bar.
                    if (!child.ParentWeb.Navigation.UseShared)
                        // Get the parent's top link bar.
                        SPNavigationNodeCollection topnav = child.ParentWeb.Navigation.TopNavigationBar;

                        // Query for an existing link to the child.
                        SPNavigationNode node = topnav
                            .FirstOrDefault(n => n.Url.Equals(child.ServerRelativeUrl));

                        // No link, so add one.
                        if (node == null)
                            // Truncate a long title.
                            string linkTitle = child.Title;
                            if (linkTitle.Length > 15)
                                linkTitle = linkTitle.Substring(0, 12) + "...";

                            // Create the node.
                            node = new SPNavigationNode(linkTitle, child.ServerRelativeUrl);

                            // Add it.
                            node = topnav.AddAsLast(node);
            Console.Write("\nPress ENTER to continue....");