共用方式為


SPNavigationNodeCollection.Add Method

Adds an SPNavigationNode object after the specified 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 Add ( _
    node As SPNavigationNode, _
    previousNode As SPNavigationNode _
) As SPNavigationNode
'Usage
Dim instance As SPNavigationNodeCollection
Dim node As SPNavigationNode
Dim previousNode As SPNavigationNode
Dim returnValue As SPNavigationNode

returnValue = instance.Add(node, previousNode)
public SPNavigationNode Add(
    SPNavigationNode node,
    SPNavigationNode previousNode
)

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 shows how to add a link at a specific position on the Quick Launch menu. The example is part of a larger project that uses a Web-scoped Feature to create a document library named Meeting Notes. The Feature includes an event handler that implements the SPFeatureReceiver class, and in the feature receiver's FeatureActivated method is code for creating the Meeting Notes library and adding a link to it under the Libraries heading on the Quick Launch menu. If there is a link to the Shared Documents library, the new link is added immediately after it. If the menu does not have a link to Shared Documents, the code makes the link to Meeting Notes the first item below the Libraries heading.

Note

The example code uses several types without qualification. For the code to compile correctly, your feature receiver class needs to import the following namespaces:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    // Get the web site where the feature is activated.
    SPWeb web = properties.Feature.Parent as SPWeb;
    if (web == null)
        return;

    // Get the Meeting Notes document library.
    SPList meetingNotes = web.Lists.TryGetList("Meeting Notes");
    if (meetingNotes == null)
    {
        // Create the library if it does not exist.
        Guid listId = web.Lists.Add("Meeting Notes", "An archive for meeting notes.", SPListTemplateType.DocumentLibrary);
        meetingNotes = web.Lists.GetList(listId, false);
    }

    // Check for an existing Quick Launch node for Meeting Notes.
    SPNavigationNode meetingNotesNode = web.Navigation.GetNodeByUrl(meetingNotes.DefaultViewUrl);

    // If a Meeting Notes node exists on Quick Launch, nothing more to do.
    if (meetingNotesNode != null)
        return;

    // Still here, so create a node for Meeting Notes.
    meetingNotesNode = new SPNavigationNode(meetingNotes.Title, meetingNotes.DefaultViewUrl);

    // Get the Libraries heading.
    SPNavigationNode librariesHeading = web.Navigation.GetNodeById((int)SPQuickLaunchHeading.Documents);

    // If the Libraries heading does not exist or it exists but has no items below it,
    // then Meeting Notes will be the first item.
    if (librariesHeading == null || librariesHeading.Children.Count == 0)
    {
        web.Navigation.AddToQuickLaunch(meetingNotesNode, SPQuickLaunchHeading.Documents);
        return;
    }

    // The Libraries heading exists. Now check for an item linking to Shared Documents.
    // If a node for Shared Documents exists, Meeting Notes will go after it.
    SPList sharedDocs = web.Lists.TryGetList("Shared Documents");
    SPNavigationNode sharedDocsNode = null;
    if (sharedDocs != null)
        sharedDocsNode = librariesHeading
            .Children
            .Cast<SPNavigationNode>()
            .FirstOrDefault(n => n.Url == sharedDocs.DefaultViewUrl);

    // A node for Shared Documents does not exist. Make Meeting Notes the first item.
    if (sharedDocsNode == null)
    {
        librariesHeading.Children.AddAsFirst(meetingNotesNode);
        return;
    }

    // A node for Shared Documents exists. Place Meeting Notes after it.
    librariesHeading.Children.Add(meetingNotesNode, sharedDocsNode);

    web.Dispose();
}
Public Overrides Sub FeatureActivated(ByVal properties As SPFeatureReceiverProperties)

    'Get the web site where the feature is activated.
    Dim web As SPWeb = TryCast(properties.Feature.Parent, SPWeb)
    If web Is Nothing Then
        Return
    End If

    ' Get the Meeting Notes document library.
    Dim meetingNotes As SPList = web.Lists.TryGetList("Meeting Notes")
    If meetingNotes Is Nothing Then

        ' Create the library if it does not exist.
        Dim listId As Guid = web.Lists.Add("Meeting Notes", "An archive for meeting notes.", SPListTemplateType.DocumentLibrary)
        meetingNotes = web.Lists.GetList(listId, False)

    End If

    ' Check for an existing Quick Launch node for Meeting Notes.
    Dim meetingNotesNode As SPNavigationNode = web.Navigation.GetNodeByUrl(meetingNotes.DefaultViewUrl)

    ' If a Meeting Notes node exists on Quick Launch, nothing more to do.
    If meetingNotesNode IsNot Nothing Then
        Return
    End If

    ' Still here, so create a node for Meeting Notes.
    meetingNotesNode = New SPNavigationNode(meetingNotes.Title, meetingNotes.DefaultViewUrl)

    ' Get the Libraries heading.
    Dim librariesHeading As SPNavigationNode = web.Navigation.GetNodeById(CInt(SPQuickLaunchHeading.Documents))

    ' If the Libraries heading does not exist or it exists but has no items below it,
    ' then Meeting Notes will be the first item.
    If librariesHeading Is Nothing OrElse librariesHeading.Children.Count = 0 Then
        web.Navigation.AddToQuickLaunch(meetingNotesNode, SPQuickLaunchHeading.Documents)
        Return
    End If

    ' The Libraries heading exists. Now check for an item linking to Shared Documents.
    ' If a node for Shared Documents exists, Meeting Notes will go after it.
    Dim sharedDocs As SPList = web.Lists.TryGetList("Shared Documents")
    Dim sharedDocsNode As SPNavigationNode = Nothing
    If sharedDocs IsNot Nothing Then
        sharedDocsNode = librariesHeading.Children.Cast(Of SPNavigationNode)().FirstOrDefault( _
            Function(n) n.Url = sharedDocs.DefaultViewUrl)
    End If

    ' A node for Shared Documents does not exist. Make Meeting Notes the first item.
    If sharedDocsNode Is Nothing Then
        librariesHeading.Children.AddAsFirst(meetingNotesNode)
        Return
    End If

    ' A node for Shared Documents exists. Place Meeting Notes after it.
    librariesHeading.Children.Add(meetingNotesNode, sharedDocsNode)

    web.Dispose()
End Sub

See Also

Reference

SPNavigationNodeCollection Class

SPNavigationNodeCollection Members

Microsoft.SharePoint.Navigation Namespace

Microsoft.SharePoint.Navigation.SPNavigationNode

SPNavigationNode.Update

Move(SPNavigationNodeCollection, SPNavigationNode)