public void MoveToLast(SPNavigationNodeCollectionInstance collection) { if (collection == null) { throw new JavaScriptException(this.Engine, "Error", "SPNavigationNodeCollection must be specified."); } m_navigationNode.MoveToLast(collection.SPNavigationNodeCollection); }
/// <summary> /// reads a set of integers /// that represents a recorded node movement and /// perform the node re-ordering /// </summary> /// <param name="movementInfo"></param> private void MoveNode(string[] movementInfo) { int parentNodeID = Convert.ToInt32(movementInfo[0]); int oldIndex = Convert.ToInt32(movementInfo[1]); //if (_appId != -1) //{ // // translate displayed index into real index in Web.Navigation // oldIndex = AppSettingsHelper.GetRealIndex(parentNodeID, oldIndex, _appId, _nodeType); //} int newIndex = Convert.ToInt32(movementInfo[2]); //if (_appId != -1) //{ // // translate displayed index into real index in Web.Navigation // newIndex = AppSettingsHelper.GetRealIndex(parentNodeID, newIndex, _appId, "topnav"); //} SPNavigationNode parentNode = _web.Navigation.GetNodeById(parentNodeID); SPNavigationNodeCollection contextNodeCollection = parentNode.Children; SPNavigationNode node = contextNodeCollection[oldIndex]; int currentlastIndex = contextNodeCollection.Count - 1; if (newIndex != oldIndex) { if (newIndex == 0) { node.MoveToFirst(contextNodeCollection); } else if (newIndex == currentlastIndex) { node.MoveToLast(contextNodeCollection); } else { if (newIndex < oldIndex) { node.Move(contextNodeCollection, contextNodeCollection[newIndex - 1]); } else { node.Move(contextNodeCollection, contextNodeCollection[newIndex]); } } } // note: // when performing SPNavigationNode.(Move/MoveToFirst/MoveToLast) // SPNavigationNode.Update() is not necessary according to MSDN documents }
/// <summary> /// reads a set of integers /// that represents a recorded node movement and /// perform the node re-ordering /// </summary> /// <param name="movementInfo"></param> private void MoveNode(string[] movementInfo) { int parentNodeID = Convert.ToInt32(movementInfo[0]); int oldIndex = Convert.ToInt32(movementInfo[1]); int newIndex = Convert.ToInt32(movementInfo[2]); _eWeb.AllowUnsafeUpdates = true; SPNavigationNode eParentNode = _eWeb.Navigation.GetNodeById(parentNodeID); SPNavigationNodeCollection eContextNodeCollection = eParentNode.Children; SPNavigationNode eNode = eContextNodeCollection[oldIndex]; int currentlastIndex = eContextNodeCollection.Count - 1; if (newIndex != oldIndex) { if (newIndex == 0) { eNode.MoveToFirst(eContextNodeCollection); } else if (newIndex == currentlastIndex) { eNode.MoveToLast(eContextNodeCollection); } else { if (newIndex < oldIndex) { eNode.Move(eContextNodeCollection, eContextNodeCollection[newIndex - 1]); } else { eNode.Move(eContextNodeCollection, eContextNodeCollection[newIndex]); } } } // note: // when performing SPNavigationNode.(Move/MoveToFirst/MoveToLast) // SPNavigationNode.Update() is not necessary according to MSDN documents }
/// <summary> /// Adds the nodes. /// </summary> /// <param name="pubWeb">The publishing web.</param> /// <param name="isGlobal">if set to <c>true</c> [is global].</param> /// <param name="existingNodes">The existing nodes.</param> /// <param name="newNodes">The new nodes.</param> private static void AddNodes(PublishingWeb pubWeb, bool isGlobal, SPNavigationNodeCollection existingNodes, XmlNodeList newNodes) { if (newNodes.Count == 0) { return; } for (int i = 0; i < newNodes.Count; i++) { XmlElement newNodeXml = (XmlElement)newNodes[i]; string url = newNodeXml.SelectSingleNode("Url").InnerText; string title = newNodeXml.GetAttribute("Title"); NodeTypes type = NodeTypes.None; if (newNodeXml.SelectSingleNode("NodeType") != null && !string.IsNullOrEmpty(newNodeXml.SelectSingleNode("NodeType").InnerText)) { type = (NodeTypes)Enum.Parse(typeof(NodeTypes), newNodeXml.SelectSingleNode("NodeType").InnerText); } bool isVisible = true; if (!string.IsNullOrEmpty(newNodeXml.GetAttribute("IsVisible"))) { isVisible = bool.Parse(newNodeXml.GetAttribute("IsVisible")); } if (type == NodeTypes.Area) { // You can't just add an "Area" node (which represents a sub-site) to the current web if the // url does not correspond with an actual sub-site (the code will appear to work but you won't // see anything when you load the page). So we need to check and see if the node actually // points to a sub-site - if it does not then change it to "AuthoredLinkToWeb". SPWeb web = null; try { string name = url.Trim('/'); if (name.Length != 0 && name.IndexOf("/") > 0) { name = name.Substring(name.LastIndexOf('/') + 1); } try { // pubWeb.Web.Webs[] does not return null if the item doesn't exist - it simply throws an exception (I hate that!) web = pubWeb.Web.Webs[name]; } catch (ArgumentException) { } if (web == null || !web.Exists || web.ServerRelativeUrl.ToLower() != url.ToLower()) { // The url doesn't correspond with a sub-site for the current web so change the node type. // This is most likely due to copying navigation elements from another site type = NodeTypes.AuthoredLinkToWeb; } else if (web.Exists && web.ServerRelativeUrl.ToLower() == url.ToLower()) { // We did find a matching sub-site so now we need to set the visibility if (isVisible) { pubWeb.Navigation.IncludeInNavigation(isGlobal, web.ID); } else { pubWeb.Navigation.ExcludeFromNavigation(isGlobal, web.ID); } } } finally { if (web != null) { web.Dispose(); } } } else if (type == NodeTypes.Page) { // Adding links to pages has the same limitation as sub-sites (Area nodes) so we need to make // sure it actually exists and if it doesn't then change the node type. PublishingPage page = null; try { // Note that GetPublishingPages()[] does not return null if the item doesn't exist - it simply throws an exception (I hate that!) page = pubWeb.GetPublishingPages()[url]; } catch (ArgumentException) { } if (page == null) { // The url doesn't correspond with a page for the current web so change the node type. // This is most likely due to copying navigation elements from another site type = NodeTypes.AuthoredLinkToPage; url = pubWeb.Web.Site.MakeFullUrl(url); } else { // We did find a matching page so now we need to set the visibility if (isVisible) { pubWeb.Navigation.IncludeInNavigation(isGlobal, page.ListItem.UniqueId); } else { pubWeb.Navigation.ExcludeFromNavigation(isGlobal, page.ListItem.UniqueId); } } } // If it's not a sub-site or a page that's part of the current web and it's set to // not be visible then just move on to the next (there is no visibility setting for // nodes that are not of type Area or Page). if (!isVisible && type != NodeTypes.Area && type != NodeTypes.Page) { continue; } // Finally, can add the node to the collection. SPNavigationNode node = SPNavigationSiteMapNode.CreateSPNavigationNode( title, url, type, existingNodes); // Now we need to set all the other properties foreach (XmlElement property in newNodeXml.ChildNodes) { // We've already set these so don't set them again. if (property.Name == "Url" || property.Name == "Node" || property.Name == "NodeType") { continue; } // CreatedDate and LastModifiedDate need to be the correct type - all other properties are strings if (property.Name == "CreatedDate" && !string.IsNullOrEmpty(property.InnerText)) { node.Properties["CreatedDate"] = DateTime.Parse(property.InnerText); continue; } if (property.Name == "LastModifiedDate" && !string.IsNullOrEmpty(property.InnerText)) { node.Properties["LastModifiedDate"] = DateTime.Parse(property.InnerText); continue; } node.Properties[property.Name] = property.InnerText; } // If we didn't have a CreatedDate or LastModifiedDate then set them to now. if (node.Properties["CreatedDate"] == null) { node.Properties["CreatedDate"] = DateTime.Now; } if (node.Properties["LastModifiedDate"] == null) { node.Properties["LastModifiedDate"] = DateTime.Now; } // Save our changes to the node. node.Update(); node.MoveToLast(existingNodes); // Should already be at the end but I prefer to make sure :) XmlNodeList childNodes = newNodeXml.SelectNodes("Node"); // If we have child nodes then make a recursive call passing in the current nodes Children property as the collection to add to. if (childNodes.Count > 0) { AddNodes(pubWeb, isGlobal, node.Children, childNodes); } } }