65.9K
CodeProject is changing. Read more.
Home

Add/Update/Delete nodes from QuickLaunch (Current Navigation)/ Global Navigation using Client Side Object Model (CSOM) in SharePoint 2013 Online

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Apr 30, 2015

CPOL
viewsIcon

21420

This article explains how nodes can be added/updated/deleted from site navigation using .Net Managed CSOM in SharePoint 2013 Online site

Modification of nodes in site navigation is a very common task which is done using UI. SharePoint Client Object model also allows adding/updating/deleting nodes in site navigation.

When working with nodes in site navigation, we need to fetch nodes collection first. This can be achieved by requesting QuickLaunch property (Left Navigation) or TopNavigationBar  property (Top Navigation) of Navigation class.

 

NavigationNodeCollection qlNavNodeColl = context.Web.Navigation.QuickLaunch;

NavigationNodeCollection topNavNodeColl = context.Web.Navigation.TopNavigationBar;

In the given samples, nodes are being modifed in left navigation only. For modification of nodes in top navigation TopNavigationBar property must be used instead of QuickLaunch.

Adding a node

private static void AddNode(string url)

        {
            ClientContext context = new ClientContext(url);

            context.Load(context.Web);

            //Fetching website's Left Navigation node collection

            NavigationNodeCollection qlNavNodeColl = context.Web.Navigation.QuickLaunch;

            //Create a new node

            NavigationNodeCreationInformation newNavNode = new NavigationNodeCreationInformation();

            newNavNode.Title = "Google";

            newNavNode.Url = "https://www.google.com"; //URL must always start with http/https

            qlNavNodeColl.Add(newNavNode);

            try
            {
                context.ExecuteQuery();
            }
            catch (Exception)
            {
                //Handle exception
            }
        }

This way the node is always added as a first node in the collection. For adding node as a last node AsLastNode property of NavigationNodeCreationInformation should be set to true.

newNavNode.AsLastNode = true;

If a node is required to be added after a particular node then PreviousNode property needs to be set to the node after which our newly created node is to be added.

private static void AddNode(string url)

        {
            ClientContext context = new ClientContext(url);

            context.Load(context.Web);

            //Fetching website's Left Navigation node collection

            NavigationNodeCollection qlNavNodeColl = context.Web.Navigation.QuickLaunch;


            IEnumerable<NavigationNode> twitterNode = context.LoadQuery(qlNavNodeColl.Where(n => n.Title == "Twitter"));

            try

            {
                context.ExecuteQuery();
            }
            catch (Exception)
            {
                //Handle exception
            }

            //Create a new node

            NavigationNodeCreationInformation newNavNode = new NavigationNodeCreationInformation();

            newNavNode.Title = "Google";

            newNavNode.Url = "https://www.google.com"; //URL must always start with http/https

            newNavNode.PreviousNode = twitterNode.FirstOrDefault();

            qlNavNodeColl.Add(newNavNode);

            try
            {
                context.ExecuteQuery();
            }
            catch (Exception)
            {
                //Handle exception
            }
        }

Modify an existing node

private static void UpdateNode(string url)

        {
            ClientContext context = new ClientContext(url);

            context.Load(context.Web);

            //Fetching website's Left Navigation node collection

            NavigationNodeCollection qlNavNodeColl = context.Web.Navigation.QuickLaunch;

            //Fetching node which needs to be updated

            IEnumerable<NavigationNode> googleNode = context.LoadQuery(qlNavNodeColl.Where(n => n.Title == "Google"));

            try
            {
                context.ExecuteQuery();
            }
            catch (Exception)
            {
                //Handle exception
            }

            if (googleNode.Count() == 1)
            {
                NavigationNode gNode = googleNode.FirstOrDefault();

                gNode.Url = "https://www.google.co.in";

                gNode.Update();

                try
                {
                    context.ExecuteQuery();
                }
                catch (Exception)
                {
                   //Handle exception
                }
            }
        }

Deleting a node

private static void DeleteNode(string url)

        {
            ClientContext context = new ClientContext(url);

            context.Load(context.Web);

            //Fetching website's Left Navigation node collection

            NavigationNodeCollection qlNavNodeColl = context.Web.Navigation.QuickLaunch;

            //Fetching node which needs to be deleted

            IEnumerable<NavigationNode> googleNode = context.LoadQuery(qlNavNodeColl.Where(n => n.Title == "Google"));

            try
            {
                context.ExecuteQuery();
            }
            catch (Exception)
            {
                //Handle exception
            }

            if (googleNode.Count() == 1)
            {
                googleNode.FirstOrDefault().DeleteObject();

                try
                {
                    context.ExecuteQuery();
                }
                catch (Exception)
                {
                    //Handle exception
                }
            }
        }