Last post, I showed you how to disable or empty the Quick Launch. In this post, I will show you how to add nodes to or delete nodes from Quick Launch programmatically. You have some options to add the nodes as first node, before another existing node, after an existing node or as last node in the navigation. Via some overloading methods, you can decide if it is going to be a parent node or a child node. The same goes for deleting nodes, delete a parent node or a child node of an existing node.
Update 17 august 2009: There were some problems with the methods below. I’ve updated all the methods.
First of all we define a small enumeration for the node position used in the AddQuickLaunchNode methods.
Public Enum NodePosition
First = 0
After = 1
Before = 2
Last = 3
End Enum
We also need some private helper methods to Add a node, Delete a node and Search a node in a SPNavigationNodeCollection, like the Quick Launch.
Private Shared Sub AddQuickLaunchNode( _
ByRef nnc As SPNavigationNodeCollection, _
ByVal nodeName As String, ByVal nodeUrl As String, _
ByVal isExternal As Boolean, _
ByVal position As NodePosition, ByVal abNodeName As String)
Select Case position
Case NodePosition.First
‘Add node as first node
nnc.AddAsFirst(New SPNavigationNode(nodeName, nodeUrl, isExternal))
Case NodePosition.After, NodePosition.Before
‘Select previous node
Dim previousNode As SPNavigationNode = _
GetQuickLaunchNode(nnc, abNodeName, False, position)
If Not previousNode Is Nothing Then
‘Add node before previousNode
nnc.Add(New SPNavigationNode(nodeName, nodeUrl, isExternal), _
previousNode)
Else
‘PreviousNode not found, add node as last node
nnc.AddAsLast(New SPNavigationNode(nodeName, nodeUrl, isExternal))
End If
Case NodePosition.Last
‘Add node as last node
nnc.AddAsLast(New SPNavigationNode(nodeName, nodeUrl, isExternal))
End Select
‘Update node collection
nnc.Parent.Update()
End Sub
Private Shared Sub DeleteQuickLaunchNode( _
ByRef nnc As SPNavigationNodeCollection, _
ByVal nodeName As String)
‘Select node
Dim node As SPNavigationNode = _
GetQuickLaunchNode(nnc, nodeName, False)
If Not node Is Nothing Then
‘Delete node
nnc.Delete(node)
End If
End Sub
Private Shared Function GetQuickLaunchNode( _
ByRef nnc As SPNavigationNodeCollection, _
ByVal nodeName As String, _
Optional ByVal createIfNotFound As Boolean = True, _
Optional ByVal position As NodePosition = NodePosition.After)
Dim node As SPNavigationNode = Nothing
Dim previousNode As SPNavigationNode = Nothing
‘Loop over all nodes in collection
For Each nn As SPNavigationNode In nnc
If nn.Title = nodeName AndAlso position = NodePosition.Before Then
‘Node found, assign previousnode and exit the for loop
node = previousNode
Exit For
ElseIf nn.Title = nodeName AndAlso position = NodePosition.After Then
‘Node found, assign currentnode and exit the for loop
node = nn
Exit For
End If
‘Set node to current node in case of position = nodeposition.before, so
‘that the node is already set when nn.title = nodename (can’t go back in nnc)
If position = NodePosition.Before Then
previousNode = nn
End If
Next
If node Is Nothing AndAlso createIfNotFound Then
‘Node not found, creating node
node = New SPNavigationNode(nodeName, “”)
nnc.Parent.Children.AddAsLast(node)
nnc.Parent.Update()
End If
Return node
End Function
Finally, we define four public methods for adding and deleting. One for a parent node add, one for a child node add, one for a parent node delete and one for a child node delete.
Public Shared Sub AddQuickLaunchNode( _
ByRef web As SPWeb, _
ByVal nodeName As String, ByVal nodeUrl As String, _
Optional ByVal isExternal As Boolean, _
Optional ByVal position As NodePosition = NodePosition.Last, _
Optional ByVal abNodeName As String = Nothing)
‘Add Quick Launch Node on web
AddQuickLaunchNode(web.Navigation.QuickLaunch, _
nodeName, nodeUrl, isExternal, position, abNodeName)
End Sub
Public Shared Sub AddQuickLaunchNode( _
ByRef web As SPWeb, _
ByVal nodeName As String, ByVal nodeUrl As String, _
ByVal parentNodeName As String, _
Optional ByVal isExternal As Boolean, _
Optional ByVal position As NodePosition = NodePosition.Last, _
Optional ByVal abNodeName As String = Nothing)
‘Select parent node
Dim parentNode As SPNavigationNode = _
GetQuickLaunchNode(web.Navigation.QuickLaunch, parentNodeName)
If Not parentNode Is Nothing Then
‘Add Quick Launch Node under parentNode on web
AddQuickLaunchNode(parentNode.Children, _
nodeName, nodeUrl, isExternal, position, abNodeName)
End If
End Sub
Public Shared Sub DeleteQuickLaunchNode( _
ByRef web As SPWeb, ByVal nodeName As String)
‘Delete Quick Launch Node on web
DeleteQuickLaunchNode(web.Navigation.QuickLaunch, nodeName)
End Sub
Public Shared Sub DeleteQuickLaunchNode( _
ByRef web As SPWeb, ByVal nodeName As String, _
ByVal parentNodeName As String)
‘Select parent node
Dim parentNode As SPNavigationNode = _
GetQuickLaunchNode(web.Navigation.QuickLaunch, parentNodeName, False)
If Not parentNode Is Nothing Then
‘Delete Quick Launch Node under parentNode on web
DeleteQuickLaunchNode(parentNode.Children, nodeName)
End If
End Sub
Just put all these methods in a shared class and you can add nodes to, delete nodes from, disable or clear the Quick Launch menu of a web with minimal effort and coding.
August 17, 2009 at 11:38
[...] can find the original post, ‘Add or delete nodes on Quick Launch’, here. [...]