Geeks With Blogs

News
About Me:
I am a Female SharePoint Developer (a platinum unicorn). I have been working with SharePoint since I attended the Portal University in 2005. I hold a BA in Computer Science from the University of Missouri - Kansas City. I love playing Rockband, organizing user group meetings, working with code, attending events as a speaker or organizer, and having bizarre conversations about geek things with cool people. If you have any comments or questions fill out the contact form and I will try my best to help.



The Guide of a MOSSLover Becky Isserman's Blog

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

Posted on Wednesday, February 27, 2008 6:26 PM | Back to top


Comments on this post: Programmatically Add and Remove Items from the Quick Launch Bar

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting Gravatar...
This solution will show those nodes which are excluded in naivgation. How to exclude those?
Left by vipasane on Apr 16, 2008 6:14 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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.
Left by MOSSLover on Apr 16, 2008 3:21 PM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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?
Left by vipasane on Apr 18, 2008 2:20 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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.
Left by MOSSLover on Apr 18, 2008 7:48 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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?
Left by Dragon on May 13, 2008 8:02 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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.
Left by MOSSLover on May 14, 2008 10:43 PM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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?
Left by bhargavi on May 20, 2008 8:27 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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.
Left by Becky Isserman on May 21, 2008 7:38 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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.
Left by bhargavi on May 22, 2008 9:29 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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.
Left by MOSSLover on May 22, 2008 5:11 PM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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.
Left by Bhargavi on May 26, 2008 12:28 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting Gravatar...
Bhargavi,
You can make it as quicklaunch=false in Onet.xml. That might work for you
Left by Sreeni on Jun 10, 2008 3:42 PM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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?
Left by bountykiller on Jun 17, 2008 9:34 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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 ?
Left by bountykiller on Jun 17, 2008 10:15 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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.
Left by MOSSLover on Jun 26, 2008 8:25 PM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting Gravatar...
I need your help here http://social.msdn.microsoft.com/Forums/en-US/sharepointcustomization/thread/695a323a-03f3-43ac-9ad4-ee931f27f11b
Left by Qassem on Sep 29, 2008 9:55 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting Gravatar...
It's wonderful work!
Left by Avinash on Dec 01, 2008 10:04 PM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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.
Left by Rob on Mar 18, 2009 5:55 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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 ?
Left by Tanmay Bari on Mar 19, 2009 9:15 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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.
Left by Marco on Jul 09, 2009 10:53 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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
Left by Bryan van Rijn on Mar 12, 2010 1:34 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting 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


?????
Left by JL on May 18, 2011 12:44 PM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting Gravatar...
hi,
simple and nice article
Left by nandini on Apr 24, 2013 8:09 AM

# re: Programmatically Add and Remove Items from the Quick Launch Bar
Requesting Gravatar...
Here is post about a generic function to add items to the quick launch.

http://chrisbarba.com/2013/07/18/how-to-add-an-item-to-the-quick-launch-in-sharepoint/

Chris
http://chrisbarba.com
Left by Chris Barba on Jul 19, 2013 10:07 AM

Your comment:
 (will show your gravatar)


Copyright © MOSSLover | Powered by: GeeksWithBlogs.net