1: /// <summary>
2: /// Adds the nodes.
3: /// </summary>
4: /// <param name="pubWeb">The publishing web.</param>
5: /// <param name="isGlobal">if set to <c>true</c> [is global].</param>
6: /// <param name="existingNodes">The existing nodes.</param>
7: /// <param name="newNodes">The new nodes.</param>
8: private static void AddNodes(PublishingWeb pubWeb, bool isGlobal, SPNavigationNodeCollection existingNodes, List<NavigationNode> newNodes)
9: { 10: if (newNodes.Count == 0)
11: return;
12:
13: for (int i = 0; i < newNodes.Count; i++)
14: { 15: NavigationNode newNodeXml = (NavigationNode)newNodes[i];
16: string url = newNodeXml.Url;
17: string title = newNodeXml.Title;
18: NodeTypes type = newNodeXml.NodeType;
19:
20: bool isVisible = true;
21:
22: if (type == NodeTypes.Area)
23: { 24: // You can't just add an "Area" node (which represents a sub-site) to the current web if the
25: // url does not correspond with an actual sub-site (the code will appear to work but you won't
26: // see anything when you load the page). So we need to check and see if the node actually
27: // points to a sub-site - if it does not then change it to "AuthoredLinkToWeb".
28: SPWeb web = null;
29: try
30: { 31: string name = url.Trim('/'); 32: if (name.Length != 0 && name.IndexOf("/") > 0) 33: { 34: name = name.Substring(name.LastIndexOf('/') + 1); 35: }
36: try
37: { 38: // pubWeb.Web.Webs[] does not return null if the item doesn't exist - it simply throws an exception (I hate that!)
39: web = pubWeb.Web.Webs[name];
40: }
41: catch (ArgumentException)
42: { 43: }
44: if (web == null || !web.Exists || web.ServerRelativeUrl.ToLower() != url.ToLower())
45: { 46: // The url doesn't correspond with a sub-site for the current web so change the node type.
47: // This is most likely due to copying navigation elements from another site
48: type = NodeTypes.AuthoredLinkToWeb;
49: }
50: else if (web.Exists && web.ServerRelativeUrl.ToLower() == url.ToLower())
51: { 52: // We did find a matching sub-site so now we need to set the visibility
53: if (isVisible)
54: pubWeb.IncludeInNavigation(isGlobal, web.ID);
55: else
56: pubWeb.ExcludeFromNavigation(isGlobal, web.ID);
57: }
58: }
59: finally
60: { 61: if (web != null)
62: web.Dispose();
63: }
64:
65: }
66: else if (type == NodeTypes.Page)
67: { 68: // Adding links to pages has the same limitation as sub-sites (Area nodes) so we need to make
69: // sure it actually exists and if it doesn't then change the node type.
70: PublishingPage page = null;
71: try
72: { 73: // Note that GetPublishingPages()[] does not return null if the item doesn't exist - it simply throws an exception (I hate that!)
74: page = pubWeb.GetPublishingPages()[url];
75: }
76: catch (ArgumentException)
77: { 78: }
79: if (page == null)
80: { 81: // The url doesn't correspond with a page for the current web so change the node type.
82: // This is most likely due to copying navigation elements from another site
83: type = NodeTypes.AuthoredLinkToPage;
84: url = pubWeb.Web.Site.MakeFullUrl(url);
85: }
86: else
87: { 88: // We did find a matching page so now we need to set the visibility
89: if (isVisible)
90: pubWeb.IncludeInNavigation(isGlobal, page.ListItem.UniqueId);
91: else
92: pubWeb.ExcludeFromNavigation(isGlobal, page.ListItem.UniqueId);
93: }
94: }
95:
96: // If it's not a sub-site or a page that's part of the current web and it's set to
97: // not be visible then just move on to the next (there is no visibility setting for
98: // nodes that are not of type Area or Page).
99: if (!isVisible && type != NodeTypes.Area && type != NodeTypes.Page)
100: continue;
101:
102: // Finally, can add the node to the collection.
103: SPNavigationNode node = SPNavigationSiteMapNode.CreateSPNavigationNode(
104: title, url, type, existingNodes);
105:
106:
107: // Now we need to set all the other properties
108:
109: // If we didn't have a CreatedDate or LastModifiedDate then set them to now.
110: if (node.Properties["CreatedDate"] == null)
111: node.Properties["CreatedDate"] = DateTime.Now;
112: if (node.Properties["LastModifiedDate"] == null)
113: node.Properties["LastModifiedDate"] = DateTime.Now;
114:
115: // Save our changes to the node.
116: node.Update();
117: node.MoveToLast(existingNodes); // Should already be at the end but I prefer to make sure :)
118:
119: }
120:
121: }
122: }