Click here to Skip to main content
15,891,513 members
Articles / Productivity Apps and Services / Sharepoint / SharePoint 2013
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
30 Apr 2015CPOL 21.2K   1  
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.

<span style="font-size: 14px;">newNavNode.AsLastNode = true;</span>

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
                }
            }
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Aon
India India
Microsoft certified technology specialist with over 5 years of experience in analyzing, designing and developing various segments of Software Development Life Cycle (SDLC), using Microsoft SharePoint 2010/2013.

Comments and Discussions

 
-- There are no messages in this forum --