Friday 8 August 2014

Global Node navigations

          Adding Navigations using CSOM  


       We always have getting problem after our migration in our sharepoint sites from either 2007 to 2010 or 2007 to 2013. The normal problem we used to get is the quick launch links will not be changed and we will be forced to go and to site settings --> Navigation and editing manually.

        By using client object model coding we can easily over come from this issue.All you need is to run the code in your visual studio and provide the site URL and the URL which you wanted to be changed.Here is the code we should run to get this done.



            ClientContext ctx = new ClientContext("https://../sites/sitename");
            ctx.Credentials = new NetworkCredential("UserName", "Password", "Domain");
            NavigationNodeCollection collNavNode = ctx.Web.Navigation.QuickLaunch;
            ctx.Load(collNavNode);
            ctx.ExecuteQuery();
            NavigationNodeCollection collChildNavNode = collNavNode[1].Children;
            ctx.Load(collChildNavNode);
            ctx.ExecuteQuery();
            string linkURL = string.Empty;
            foreach( NavigationNode  item in collChildNavNode)
            {
                ctx.Load(item);
                ctx.ExecuteQuery();
                if (item.Url.Contains("https://../sites"))
                {
                    linkURL = item.Url;
                    string url = linkURL.Replace("https://../sites", "/sites");
                    item.Url = url;
                }               
               
                item.Update();
                ctx.ExecuteQuery();
            }


    collNavNode[1] == Here the 1 represents the order . For example usually we have libraries in the top so that is in the 0 th location and if we want to change the quicklaunch for libraries you need to give 0 inside the bracket. When you debug this code using quick watch you can easily find the order of items.


I hope this helps in a better way!


Learn it Love it Lead it !!!


To learn more just like our page in facebook Sharepoint Dev



No comments:

Post a Comment