Exemplo n.º 1
0
        /*
         * public SPNavigationNode AddHeading(string title, string url, string group)
         * {
         *  if (!url.Equals("javascript:void(0)"))
         *  {
         *      Uri uri = new Uri(url);
         *      return AddHeading(title, uri, group);
         *  }
         *  else
         *  {
         *      if (nodes != null)
         *      {
         *          SPNavigationNode nodeItem = SPNavigationSiteMapNode.CreateSPNavigationNode(title, url, Microsoft.SharePoint.Publishing.NodeTypes.Heading, nodes);
         *          nodeItem.Properties.Add("Audience", ";;;;" + group);
         *          nodeItem.Update();
         *
         *          return nodeItem;
         *      }
         *
         *      return null;
         *  }
         * }
         *
         * public SPNavigationNode AddHeading(string title, Uri url, string group)
         * {
         *  if (nodes != null)
         *  {
         *      SPNavigationNode nodeItem = SPNavigationSiteMapNode.CreateSPNavigationNode(title, url.ToString(), Microsoft.SharePoint.Publishing.NodeTypes.Heading, nodes);
         *      nodeItem.Properties.Add("Audience", ";;;;" + group);
         *      nodeItem.Update();
         *
         *      return nodeItem;
         *  }
         *
         *  return null;
         * }
         */
        #endregion

        public SPNavigationNode AddHeading(string title, string url, string group)
        {
            if (nodes != null)
            {
                SPNavigationNode nodeItem = SPNavigationSiteMapNode.CreateSPNavigationNode(title, url, Microsoft.SharePoint.Publishing.NodeTypes.Heading, nodes);
                nodeItem.Properties.Add("Audience", ";;;;" + group);
                nodeItem.Update();

                return(nodeItem);
            }
            return(null);
        }
        /// <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);
                }
            }
        }