SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

Programmatically Add and Remove Items from the Quick Launch Bar

 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: http://www.sharepointblogs.com/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.


Posted 01-23-2008 8:28 AM by MOSSLover

Comments

WSSProgrammer wrote re: Programmatically Add and Remove Items from the Quick Launch Bar
on 01-24-2008 12:38 AM

Thanks for posting the code above - it was really helpful. Can you please post the script that dynamically replaces specific text through the master file?? Thanks in Advance -

SharePoint Link Love: 01-24-2008 at Virtual Generations wrote SharePoint Link Love: 01-24-2008 at Virtual Generations
on 01-24-2008 1:35 AM

Pingback from  SharePoint Link Love: 01-24-2008 at  Virtual Generations

Links (1/24/2008) « Steve Pietrek’s SharePoint Stuff wrote Links (1/24/2008) « Steve Pietrek’s SharePoint Stuff
on 01-24-2008 8:18 PM

Pingback from  Links (1/24/2008) « Steve Pietrek’s SharePoint Stuff

Koena wrote re: Programmatically Add and Remove Items from the Quick Launch Bar
on 02-07-2008 3:11 AM

I wanted to hide some of the links in QuickLaunch bar when it renders the pages.

remove items from add wrote remove items from add
on 07-09-2008 10:25 PM

Pingback from  remove items from add

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.