Export (0) Print
Expand All

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

public SPNavigationNode Add(
	SPNavigationNode node,
	SPNavigationNode previousNode
)

Parameters

node
Type: Microsoft.SharePoint.Navigation.SPNavigationNode
The node object (SPNavigationNode) to add to the collection.
previousNode
Type: Microsoft.SharePoint.Navigation.SPNavigationNode
Specifies the position in the node collection at which to add the new node object by identifying the node previous to the position where the new node is inserted.

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

NoteNote

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();
}

Community Additions

ADD
Show:
© 2014 Microsoft