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
                        quickLaunchNodes[6].Children.Delete(quickLaunchNodes[6].
                        Children[0]);
                        quickLaunchNodes[6].Children.Delete(quickLaunchNodes[6].
                        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);

                        quickLaunchNodes2[6].Children.AddAsLast(externalSubMenuItem1);

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

                        quickLaunchNodes2[6].Children.AddAsLast(externalSubMenuItem2);

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

                       quickLaunchNodes2[6].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.

Re-Posted from Old Blog

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Wednesday, February 27, 2008 6:26 PM

Feedback

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by vipasane at 4/16/2008 6:14 AM
Gravatar This solution will show those nodes which are excluded in naivgation. How to exclude those?

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by MOSSLover at 4/16/2008 3:21 PM
Gravatar Here is a little code example:
SPWeb Web = SPContext.Current.Web;
SPList List = Web.Lists["Your List Name"];

//needs to be here so you can update the web
Web.AllowUnsafeUpdates = true;

//adds list to quick launch
List.OnQuickLaunch = true;

//must call to update list or library
List.Update();

This is some rough code you probably want some exception handling and such.

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by vipasane at 4/18/2008 2:20 AM
Gravatar I have a site and I've checked that it shouldn't be shown in navigation. And if I use code above to make quick launch that site will be included in nodes.
Is there some property which says wheater if this site has been excluded in navigation or how to manage?
Is the only option to make a separate list which has all excluded nodes and iterate it through while making quick launch?

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by MOSSLover at 4/18/2008 7:48 AM
Gravatar I found this code off a post:
web.Navigation.QuickLaunch[i].IsVisible = false; web.Update();

What you can probably do is look at the header or url in the navigation node and then hide that node.

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by Dragon at 5/13/2008 8:02 AM
Gravatar This is good stuff. Thanks! I can definitely use this. Now I have a question for you. How can one programmatically make the Quick Launch minimize and expand either horizontally or vertically, similarly to minimizing a web part?. I know how to hide it programatically with a little css in the Content Editor Web Part and this blog is good for quick add/remove changes to the contents but minimizing it like a true web part is what I'm looking for. Can you help?

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by MOSSLover at 5/14/2008 10:43 PM
Gravatar I will have some answers for you in the upcoming weeks, because we are going to implement something similar. In the meantime someone told me you could probably do it in javascript. You probably could get rid of the default control and write a web user control or web part that would implement a menu looping through all the quick launch nodes. I promise when I figure this out that I will post a solution.

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by bhargavi at 5/20/2008 8:27 AM
Gravatar Hi,.. thanks for your posting, but i have a different situation, i need to hide some items in the quick launch based on user permissions.Is it possible to hide?

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by Becky Isserman at 5/21/2008 7:38 AM
Gravatar If you setup SharePoint Groups and add the AD Groups or users to the quick launch that you want to give the right to see that item. Then I believe there is a place in the object model to set the audiences for that item.

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by bhargavi at 5/22/2008 9:29 AM
Gravatar ok thanks.. i will try my luck with audiences, but is there a way to hide the items in the quick launch, i tried using "isVisible" property but that didnot work for me.

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by MOSSLover at 5/22/2008 5:11 PM
Gravatar What exactly are you trying to hide? If you want to hide something from everyone and it's a list or library, then you can do it in the list or library. If it's a page or site, then you can use the .visible functionality. If you want it hidden from certain users, then the audiences are good for you. It all depends on the situation.

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by Bhargavi at 5/26/2008 12:28 AM
Gravatar ok let me explain -
i want to hide the links - discussions,sites , surveys and lists from the quick launch. The approach i used was to get all the nodes of the quick launch, and get the node corresponding to the above mentioned links and said - node.isVisible=false. This did not work for me.

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by Sreeni at 6/10/2008 3:42 PM
Gravatar Bhargavi,
You can make it as quicklaunch=false in Onet.xml. That might work for you

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by bountykiller at 6/17/2008 9:34 AM
Gravatar Same Problem Here. I wanted to create my own menu, taking node from the current navigation, but the "IsVisible" property is true for each nodes, even the hidded ones. Anyone has a solution?

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by bountykiller at 6/17/2008 10:15 AM
Gravatar Same Problem Here. I wanted to create my own menu, taking node from the current navigation, but the "IsVisible" property is true for each nodes, even the hidded ones. Anyone has a solution ?

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by MOSSLover at 6/26/2008 8:25 PM
Gravatar What you could do is build a custom control or web part and delete the default control. Using a AspMenu Control in the SharePoint Object Model. You could then hide the control in the ondatabound using css or create some type of security where the control does not show up in the page load event.

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by Qassem at 9/29/2008 9:55 AM
Gravatar I need your help here http://social.msdn.microsoft.com/Forums/en-US/sharepointcustomization/thread/695a323a-03f3-43ac-9ad4-ee931f27f11b

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by Avinash at 12/1/2008 10:04 PM
Gravatar It's wonderful work!

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by Rob at 3/18/2009 5:55 AM
Gravatar Hi,
Thanks for your code. It helped me with the solution of my problem. In my situation I ran into a problem with adding (internal) links under, e.g., the sites heading. On my blog I add a post about this, because it's slightly different.

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by Tanmay Bari at 3/19/2009 9:15 AM
Gravatar Same Problem Here. I wanted to create my own menu, taking node from the current navigation, but the "IsVisible" property is true for each nodes, even the hidded ones. Anyone has a solution ?

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by Marco at 7/9/2009 10:53 AM
Gravatar someone know how get information about quicklaunch using sharepoint's Webservices??
maybe using Sites.asmx or SiteData.asmx.
I need only get this information.
Thanks.

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by Bryan van Rijn at 3/12/2010 1:34 AM
Gravatar Hi there,

For all the copy pasters in here, there is still a memory leak:
SPWeb quickLaunchWeb = site.OpenWeb();

Should be:

using(SPWeb quickLaunchWeb = site.OpenWeb())
{
..... Code here
}
Happy coding

# re: Programmatically Add and Remove Items from the Quick Launch Bar

Left by JL at 5/18/2011 12:44 PM
Gravatar I've created a team site programmatically in sharepoint 2010 and also created a header, in case is needed, on the parent site and then added a link to the new site, but for some reason, oSPWeb.update() just create the new site but the new header or link doesn't appear in the quicklaunch.

AllowUnsafeUpdates = true


?????

Your comment:





 
 

Copyright © MOSSLover

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski