Add node to QuickLaunch

24/03/2014 11:35

   static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://yoursitecollection"))
            {
                using (SPWeb web = site.OpenWeb())
                {

                    SPNavigationNode hd1Node = AddHeadingToQuickLaunch(web, "First heading");
                    AddLinkToQuickLaunch(hd1Node, "First link", "SitePages/page1.aspx", false);
                    AddLinkToQuickLaunch(hd1Node, "Second link", "SitePages/page2.aspx", false);
                    AddLinkToQuickLaunch(hd1Node, "Third link", "SitePages/page3.aspx", false);

                    AddPageToNavigation(web, "Heading 1 with link", "Documents/Forms/AllItems.aspx");
                
                    SPNavigationNode hd2Node = AddHeadingToQuickLaunch(web, "Second heading");
                    AddLinkToQuickLaunch(hd2Node, "sublink1", "SitePages/otherpage.aspx", false);
                    AddLinkToQuickLaunch(hd2Node, "Google", "https://www.google.com", true);
                                  


                    //Remove created nodes
                    //RemoveNavigationNodeFromQuickLaunch(web, "First heading");
                    //RemoveNavigationNodeFromQuickLaunch(web, "Heading 1 with link");
                    //RemoveNavigationNodeFromQuickLaunch(web, "Second heading");
               
                }
            }
        }

        public static void RemoveNavigationNodeFromQuickLaunch(SPWeb web, string nodeTitle)
        {
            //Add page to Quick Launch navigation
            SPNavigationNodeCollection nodes = web.Navigation.QuickLaunch;
            SPNavigationNode nodeToRemove = nodes.OfType<SPNavigationNode>().FirstOrDefault(n => n.Title == nodeTitle);
            if (nodeToRemove != null)
            {
                web.Navigation.QuickLaunch.Delete(nodeToRemove);
            }
        }

        public static void AddPageToNavigation(SPWeb web, string nodeTitle, string nodeUrl)
        {
            //Add page to Quick Launch navigation
            SPNavigationNodeCollection nodes = web.Navigation.QuickLaunch;
            SPNavigationNode node = nodes.OfType<SPNavigationNode>().FirstOrDefault(n => n.Title == nodeTitle);

            if (node == null)
            {
                node = new SPNavigationNode(nodeTitle, nodeUrl, false);
                node = web.Navigation.QuickLaunch.AddAsLast(node);
                node.Update();
            }
          
        }

        public static SPNavigationNode AddLinkToQuickLaunch(SPNavigationNode parentNode, string title, string url, bool isExternal)
        {
            SPNavigationNodeCollection nodes = parentNode.Children;
            SPNavigationNode node = nodes.OfType<SPNavigationNode>().FirstOrDefault(n => n.Title == title);

            if (node == null)
            {
                node = new SPNavigationNode(title, url, isExternal);
                parentNode.Children.AddAsLast(node);       // refresh navigation node just in case    
                node = parentNode.Navigation.GetNodeById(node.Id);
            }
            return node;
        }

        public static SPNavigationNode AddHeadingToQuickLaunch(SPWeb web, string headingName)
        {
            SPNavigationNodeCollection quicklaunchNav = web.Navigation.QuickLaunch;
            SPNavigationNode headingNode = quicklaunchNav.OfType<SPNavigationNode>().FirstOrDefault(n => n.Title == headingName);

            if (headingNode == null)
            {
                headingNode = new SPNavigationNode(headingName, "javascript:window.goback(0)", true);
                headingNode = quicklaunchNav.AddAsLast(headingNode);       //turn the node into Heading
                TurnIntoHeading(headingNode);
                headingNode.Update();       // refresh navigation node just in case   
                headingNode = web.Navigation.GetNodeById(headingNode.Id);
            }
            return headingNode;
        }

        public static void TurnIntoHeading(SPNavigationNode node)
        {
            if (node != null)
            {
                node.Properties["NodeType"] = "Heading";
                node.Properties["BlankUrl"] = "True";
                node.Properties["LastModifiedDate"] = DateTime.Now;
                node.Properties["Target"] = "";
                node.Properties["vti_navsequencechild"] = "true";
                node.Properties["UrlQueryString"] = "";
                node.Properties["CreatedDate"] = DateTime.Now;
                node.Properties["Description"] = "";
                node.Properties["UrlFragment"] = "";
                node.Properties["Audience"] = "";
            }
        }