Setting the navigation is another common post upgrade/deployment task that should be easy to script. One of the challenges in using the commands I created was that it's necessary to know the node ID of the navigational elements. There's probably ways to change this to use the name rather than the ID but you can run into trouble when dealing with items with the same name. To set the navigation using the web interface go to your top level site ->Site Settings->Navigation.
This one took me a while to figure out as the code that Microsoft wrote to handle this was cryptic at best. It was very difficult to wade through some of the oddness. In the end I figured out that you simply had to get a PublishingWeb object and manipulate the GlobalNavigationNodes collection or the CurrentNavigationNodes collection depending on your intentions. In order to add a new node you need to know the existing node IDs so I created an enumerate command to get that information (again there's probably another way to approach this but this worked for my needs). The two commands I created are called gl-enumnavigation and gl-addnavigationnode.
The code for this turned out to be pretty simple. The main part of the code is a recursive routine which goes through all the nodes and their children. I added some indenting to give it more of a tree layout (UPDATE 9/4/2007: I added an xml switch to allow the output to be specified as XML instead of flat text - the results can also be dumped to an output file specified via an outputfile parameter).
As you can see from the code above, the EnumerateCollection static method does most of the work. It takes in an SPNavigationNodeCollection and iterates through the collection, passing any children back into the same method recursively for processing. The syntax of the command can be seen below:
C:\>stsadm -help gl-enumnavigation stsadm -o gl-enumnavigation Returns the site navigation hierarchy. Parameters: -url <site collection> [-xml] [-outputfile <file to output results to>
Here’s an example of how to return the site navigation:
stsadm –o gl-enumnavigation –url "http://intranet/"
The results of running the above command can be seen below (note that I'm also output all the properties associated with each node object which can be useful when trying to determine how the various NodeType enums are assigned:
C:\>stsadm -o gl-enumnavigation -url "http://intranet/ Global Navigation: - 2018: Topics :NodeType=Area :vti_navsequencechild=true - 2019: News :NodeType=Area :vti_navsequencechild=true - 2021: Sites :NodeType=Area :vti_navsequencechild=true Current Navigation: - 2013: Topics :NodeType=Area :vti_navsequencechild=true - 2014: News :NodeType=Area :vti_navsequencechild=true - 2016: Sites :NodeType=Area :vti_navsequencechild=true
Figuring out how to add a new node took a little more digging. Turns out that you need to use SPNavigationSiteMapNode.CreateSPNavigationNode() which is a static method that returns back an SPNavigationNode object. You pass into this static method the name, link, type, and nodes collection that the new node belongs to (global or current). Once the node is created you set the remaining properties and call Update(). At this point the node exists as the last item - now you simply move it to where you want (no need to update again):
The syntax of the command can be seen below:
C:\>stsadm -help gl-addnavigationnode stsadm -o gl-addnavigationnode Adds a new node to the site hierarchy. Parameters: -url <site collection url> -name <navigation name> -linkurl <link url> -type <link type: None | Area | Page | List | ListItem | PageLayout | Heading | AuthoredLinkToPage | AuthoredLinkToWeb | AuthoredLinkPlain | AuthoredLink | Default | Custom | All> -global / -current -addasfirst / -addaslast / -addafter <previous node id> [-newwindow] [-description <description text>]
Here’s an example of how to add a new node after an existing node:
stsadm –o gl-addnavigationnode–url "http://intranet/" -name "Help Desk" -linkurl "http://helpdesk/" -type AuthoredLinkPlain -global -addafter 2019 -newwindow -description "Corporate Help Desk Application"