You are here: Home » Programmatically Add and Remove Items from the Quick Launch Bar

Programmatically Add and Remove Items from the Quick Launch Bar

Posted by mosslover
No Comments »

 I ran into a problem last week, where I need to update multiple sites navigation really quick by deleting and adding items to the navigation on the quick launch bar.  I found this great entry from Todd Baginski: /tbaginski/archive/2007/12/26/how-to-programmatically-customize-site-navigation-in-wss-3-0-and-moss-2007.aspx.  I wanted to take this a step further by removing nodes from underneath a specific heading.  Then I wanted to re-add different nodes, so that I could pull up some javascript that would change the links based on certain phrases (i.e. http://reports, http://sql/[Specific Annual Report]).  Here is the sample code for the quick launch additions and removals:

 SiteName = "your site name here";

                    using (SPSite site = new SPSite(SiteName))
                    {

                        SPWeb quickLaunchWeb = site.OpenWeb();

                        SPNavigationNodeCollection quickLaunchNodes = quickLaunchWeb.Navigation.QuickLaunch;
                       
                        //deletes the first and second child underneath the 7th heading on the page
                        quickLaunchNodesDevil.Children.Delete(quickLaunchNodesDevil.Children[0]);
                        quickLaunchNodesDevil.Children.Delete(quickLaunchNodesDevil.Children[1]);

//Notice that deleting any children is easy as long as you know the position…You can probably also check the children's names too and create a dynamic component and loop through the quick launch list and delete items by adding an if or for loop.

                        //creates 5 children underneath the 7th heading
                        SPNavigationNodeCollection quickLaunchNodes2 = quickLaunchWeb.Navigation.QuickLaunch;

                        SPNavigationNode externalSubMenuItem1 = new SPNavigationNode("Link1", "Url1", true);

                        quickLaunchNodes2Devil.Children.AddAsLast(externalSubMenuItem1);

                        SPNavigationNode externalSubMenuItem2 = new SPNavigationNode("Link2", "Url2", true);

                        quickLaunchNodes2Devil.Children.AddAsLast(externalSubMenuItem2);

                        SPNavigationNode externalSubMenuItem3 = new SPNavigationNode("Link3", "Url3", true);

                       quickLaunchNodes2Devil.Children.AddAsLast(externalSubMenuItem3);

                        quickLaunchWeb.Navigation.UseShared = true;

                         //You only need one update statement for all the remove and adds, on first shot I accidentally added it twice and learned this lesson.
                         quickLaunchWeb.Update();
                    }

I came up with this program really quick and it was a console application.  I added the first part of the site and had a console.readline statement to type in the site number, because ours were numerically based.  I wanted to double check every site to see if anything was broken in other areas.  If you want a quicker version, then you could probably just run any site in that specific web or read from an outside data source.  For example,  we have a web service that lists all the sites I could have called the web service to get the number and add/remove items from the quick launch with one go at the program.  If you guys would like a script example on how to use these items that I added to the quick launch to change the links with one file let me know.  I have a script that dynamically replaces specific text through the master file.  I hope that you guys enjoy.

Your email is never shared.
Required fields are marked *




Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>