다음을 통해 공유


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)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Function AddAsLast ( _
    node As SPNavigationNode _
) As SPNavigationNode
'Usage
Dim instance As SPNavigationNodeCollection
Dim node As SPNavigationNode
Dim returnValue As SPNavigationNode

returnValue = instance.AddAsLast(node)
public SPNavigationNode AddAsLast(
    SPNavigationNode node
)

Parameters

Return Value

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

Remarks

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).

Examples

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("https://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
                            .Cast<SPNavigationNode>()
                            .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....");
            Console.ReadLine();
        }
    }
}
Imports System
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Navigation

Module ConsoleApp

    Sub Main()

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

            Using child As SPWeb = 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 Not child.ParentWeb.Navigation.UseShared Then

                    ' Get the parent's top link bar.
                    Dim topnav As SPNavigationNodeCollection = child.ParentWeb.Navigation.TopNavigationBar

                    ' Query for an existing link to the child.
                    Dim node As SPNavigationNode = topnav.Cast(Of SPNavigationNode)().FirstOrDefault(Function(n) n.Url.Equals(child.ServerRelativeUrl))

                    ' No link, so add one.
                    If node Is Nothing Then
                        ' Truncate a long title.
                        Dim linkTitle As String = child.Title
                        If linkTitle.Length > 15 Then
                            linkTitle = linkTitle.Substring(0, 12) + "..."
                        End If

                        ' Create the node.
                        node = New SPNavigationNode(linkTitle, child.ServerRelativeUrl)

                        ' Add it.
                        node = topnav.AddAsLast(node)
                    End If

                End If

            End Using

        End Using

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

End Module

See Also

Reference

SPNavigationNodeCollection Class

SPNavigationNodeCollection Members

Microsoft.SharePoint.Navigation Namespace

Microsoft.SharePoint.Navigation.SPNavigationNode

SPNavigationNode.Update

UseShared

MoveToLast(SPNavigationNodeCollection)