Dec032008

How to update Quick Launch links programmatically in SharePoint and WSS 3.0

Published by nauman at 11:53 AM under SharePoint

Updating quick launch navigation in SharePoint is much simpler with WSS 3.0 Microsoft.SharePoint.Navigation API. Please refer to the following sample code that will help you to iterate through all quick launch links in SharePoint sites and update them as per your liking.

 private static void Traverse(string top, string from, string to)

        {
 
             SPSite topSite;
             SPWebCollection subSites;
 
            try
            {
 
                topSite = new SPSite(top);
                subSites = topSite.AllWebs;
 
 
                foreach (SPWeb web in subSites)
                {
                    using (web)
                    {
                        Console.WriteLine("******** STARTING " + web.Name.ToString() + "************");
                        SPNavigationNodeCollection nodes = web.Navigation.QuickLaunch;
 
                        foreach (SPNavigationNode node in nodes)
                        {
 
                            UpdateURL(node, from, to);
 
                            SPNavigationNodeCollection childNodes = node.Children;
 
                            foreach (SPNavigationNode cNode in childNodes)
                            {
                                UpdateURL(cNode, from, to);
 
                            }
 
                        }
 
                        Console.WriteLine("******** CLOSING " + web.Name.ToString() + "************");
                    }
                }
              //  Console.ReadLine();
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message + " :: " + exp.StackTrace.ToString());
            }
            
 
        }
 
        private static void UpdateURL(SPNavigationNode cNode, string from, string to)
        {
            
 
            if (cNode.Url.IndexOf(from) > 0)
            {
                Console.WriteLine("FOUND CULPRIT -> " + cNode.Title.ToString() + " :: " + cNode.Url.ToString());
                cNode.Url = cNode.Url.ToString().Replace(from, to);
                cNode.Update();
                Console.WriteLine(cNode.Title.ToString() + " :: " + cNode.Url.ToString());
            }
 
        }

enJoY!



[KickIt] [Dzone] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags: ,

E-mail | Permalink | Trackback | Post RSSRSS comment feed 1 Responses

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Don't wait! Share your blog with the whole world and get new readers for your posts in real time now!