1: private void InsertNavigationNodeToCollectionAsChild(SPWeb rootWeb,
2: SPNavigationNode nodeToInsert,
3: SPNavigationNodeCollection targetNodeCollection,
4: string parentNodeTitle)
5: {
6: bool hasParentNode = false;
7: foreach (SPNavigationNode node in targetNodeCollection)
8: {
9: if (parentNodeTitle == node.Title)
10: {
11: //Found the required parent node, add to it,
12: //set up our test variable, update and break from the loop
13: hasParentNode = true;
14: node.Children.AddAsFirst(nodeToInsert);
15: node.Update();
16: break;
17: }
18: }
19: if (!hasParentNode)
20: {
21: //the desired parent node doesn't exist so create it ;o)
22: //create as external so that no attempt to maintain is made by WSS
23: SPNavigationNode coursesNode = new SPNavigationNode(parentNodeTitle, "", true);
24:
25: //add the parent at the end of the targetNodeCollection
26: targetNodeCollection.AddAsLast(coursesNode);
27: coursesNode.Update();
28: //Add our new site collection node to it's parent
29: coursesNode.Children.AddAsFirst(nodeToInsert);
30: nodeToInsert.Update();
31: }
32: //call update to make everything 'stick'
33: rootWeb.Update();
34: }