Thursday 9 March 2017

Programmatically Update Quick Launch Sharepoint 2007

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;

namespace UpdateQL
{
    class Program
    {
        static void Main(string[] args)
        {
             
            int i = 0;
            string ListName = "";
            //string ListName = "ess";
            Int32 intStartItmID = 32;
            Int32 intEndItmID = 32;


            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(""))
                {
                    SPWeb web = site.OpenWeb();
                    SPList spList = web.Lists[ListName];
                    SPQuery spQItems = new SPQuery();
                    spQItems.Query = "<Where>   <And>  <Geq>  <FieldRef Name='ID' /><Value Type='Counter'>" + intStartItmID + "</Value> " +
                                   "  </Geq>         <Leq><FieldRef Name='ID' />     <Value Type='Counter'>" + intEndItmID + "</Value>   " +
                                   "  </Leq>               </And>       </Where>";
                    SPListItemCollection items = spList.GetItems(spQItems);
                    if (items.Count > 0)
                    {
                        i = items.Count;
                        foreach (SPListItem item in items)
                        {

                            if (item["Vessel_x0020_Control_x0020_Link"] != null)
                            {
                                SPFieldUrlValue spfuv = new SPFieldUrlValue(item["Vessel_x0020_Control_x0020_Link"].ToString());
                                using (SPSite subSite = new SPSite(spfuv.Url))
                                {
                                    using (SPWeb subWeb = subSite.OpenWeb())
                                    {
                                        SPNavigationNodeCollection nodeColl = subWeb.Navigation.QuickLaunch;
                                        int count = nodeColl.Count;
                                        foreach (SPNavigationNode heading in nodeColl)
                                        {
                                           
                                            foreach (SPNavigationNode links in heading.Children)
                                            {
                                                int iHeadCount = heading.Children.Count;  
                                                if (links.Title == "Resources internal")
                                                {
                                                    links.Delete();
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }

                            }

                            i = i - 1;
                        }
                    }

                }
            });
        }
       
    }
    public class URL
    {
        public 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());
            }

        }
    }
}

No comments:

Post a Comment