Adding Navigation Links To Quick Launch Bar Programatically (SharePoint 2010)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.Publishing;

namespace ConsoleOperations
{
    class AddingNavigationLinks
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost:5030"))
            {
                site.AllowUnsafeUpdates = true;

                using (SPWeb web = site.RootWeb)
                {

                    SPNavigationNodeCollection nodeColl = web.Navigation.QuickLaunch;               
                    SPNavigationNode navNode = null;
                    bool isDemandBaseNavigationGroupExists = false;

                    for (int i = nodeColl.Count - 1; i >= 0; i--)
                    {
                        navNode = nodeColl[i];
                        if (navNode.Title.Contains("DemandBase"))
                        {
                            isDemandBaseNavigationGroupExists = true;
                            break;
                        }
                    }

                    if (!isDemandBaseNavigationGroupExists)
                    {
                        SPNavigationNode quickLaunchHeader = new SPNavigationNode("HeaderNodeName", string.Empty, true);
                        quickLaunchHeader = nodeColl.AddAsLast(quickLaunchHeader);
                        quickLaunchHeader.MakeHeaderNode();
                        SPNavigationNode quickLaunchItem = new SPNavigationNode("ChildNodeName1", @"/Lists/AudienceRequests/Admin%20View.aspx", false);
                        SPNavigationNode quickLaunchItem1 = new SPNavigationNode("ChildNodeName2", @"/Lists/AudienceRequests/AllItems.aspx", false);
                        SPNavigationNode quickLaunchItem2 = new SPNavigationNode("ChildNodeName3", @"/Lists/Audience%20Request%20Tasks/AllItems.aspx", false);
                        quickLaunchHeader.Children.AddAsFirst(quickLaunchItem2);
                        quickLaunchHeader.Children.AddAsFirst(quickLaunchItem1);
                        quickLaunchHeader.Children.AddAsFirst(quickLaunchItem);
                        web.Dispose();
                    }

                    //Deletion Code....

                    //SPNavigationNode navNode = null;
                    //for (int i = nodeColl.Count - 1; i >= 0; i--)
                    //{
                    //    navNode = nodeColl[i];
                    //    if (navNode.Title.Equals("Demand Base"))
                    //    {
                    //        navNode.Delete();
                    //        Console.WriteLine("Deleted");
                    //    }

                    //    navNode = null;
                    //}
                    //web.Dispose();
                }
            }
        }
    }

// This Class Converts The Navigation Node To Header Node
    public static class SPNavigationNodeExtensions
    {
        public static void MakeHeaderNode(this SPNavigationNode node)
        {
            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["NodeType"] = "Heading";
            node.Properties["Audience"] = "";
            node.Update();
        }
    }
}

Comments

Popular posts from this blog

SharePoint 2010: JavaScript Code To Check if user exists in a group (ECMA)

How to copy DLLs from GAC.