Tuesday, July 31, 2012

SharePoint: Add Navigation Node using CSOM

Client side object model can update navigation node remotely.  Below is code to add element to top navigation.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;

namespace NavigationCustomAction
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext context = new ClientContext("http://goazrapp18:33369");
            Web web = context.Web;
            AddNavigationItem(context, web);


            Console.WriteLine("Please press any key to exit");
            Console.ReadLine();
        }

        private static void AddNavigationItem(ClientContext context, Web web)
        {
            NavigationNodeCreationInformation navigationNodeCreationInformation = new NavigationNodeCreationInformation();
            navigationNodeCreationInformation.Title = "Custom Node";
            navigationNodeCreationInformation.Url = "http://gooogle.com";
            navigationNodeCreationInformation.IsExternal = true;
            navigationNodeCreationInformation.AsLastNode = true;
            web.Navigation.TopNavigationBar.Add(navigationNodeCreationInformation);
            web.Update();
            context.ExecuteQuery();
            Console.WriteLine("Node created Successfully");
        }
    }
}


If you want to add to Quick Launch bar, you have to update one line as below.

web.Navigation.QuickLaunch.Add(navigationNodeCreationInformation);

This will add element at Quick Launch bar as well.




No comments:

Post a Comment