Last week I did a post on feature receivers. In this post, I'm going to show you how to manipulate the quicklaunch bar using a feature receiver. Now, since I already did the step by step in last week's post, I'm only going to go through the code this time. Click the link above if you need a refresher on feature receivers.
Ok, let's take a look at the code. As we did last week, we created a class and inherited from SPFeatureReceiver. In this example, only FeatureActivated and FeatureDeactivating are overriden. In FeatureActivated, a spweb object is created using the property passed to the method. Next, a SPNavigationNodeCollection is created using web.Navigation.QuickLaunch. As I'm sure you already guessed, this object is a collection of nodes used in the QuickLaunch. Now, we need an SPNavigationNode to add to the QuickLaunch. In the FeatureActivated method, 2 nodes are created. The first node is just used as a header. 3 parameters are passed; Title, Url and a boolean. True indicates that the url is an external url.
Notice that both SPNavigationNodes are added to the quicklaunch differently. The first is using the .AddAsFirst method. This will create a node at the top of the QuickLaunch's node collection (this becomes node 0). The second node is going to be a submenu item to the first node. Since the first node is 0, we use quickLaunch[0].Children.AddAsFirst(quickLaunchItem). This line will grab the 1st node, and add our node to its children.
The FeatureDeactivating method is used to remove these nodes from the quicklaunch. If you look at the code, you'll see that I'm going through the nodeCollection backwards. The reason I start from the end is because if I used a foreach loop, once the first node is deleted, the count gets thrown off and an error is generated. (Go ahead, give it a try and see what I mean).

Now, before I activate my feature, my quickLaunch looks like this:

After activation, it will look like this:

Simple huh? Oh, and if you want to manipulate the links in the top navigation bar (the tabs above your quicklaunch bar and content area, you'll still use the SPNavigationNodeCollection but instead of using web.Navigation.QuickLaunch, you'll want to use web.Navigation.TopNavigationBar.Labels: Developer