Case about sorting the quick launch navigation
Many things are easy to do in SharePoint. Considerable stuffs are tricky. And a few – difficult/impossible. One such tricky stuff is customizing navigation (both quick launch and top navigation).
Recently, I had an internal request regarding customizing quick launch navigation nodes. There are a few good articles out on the internet that talks about leveraging what’s provided via SharePoint OM in terms of customizing/manipulating navigation nodes. But I didn’t see one (perhaps I am still not an expert in www.live.com search) that talks about sorting navigation nodes in some order.
After considerable research, I realized the best way to go about this would be “back-to-basics”. Arrays, ArrayLists, HashTable did not seem to be able to gel well with SPNavigationNode[] object. If you try “cast”, it will work with Array, ArrayList types, but you’ll hit a roadblock when trying to use Sort() method these types expose.
So, below is the “bubble-sort” algorithm that sorted this case out:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;
namespace SortNavigation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SPSite site = new SPSite(" <your sharepoint site url> "))
{
using (SPWeb web = site.OpenWeb())
{
try
{
SPNavigationNodeCollection nodes = web.Navigation.QuickLaunch;
for (int i = 0; i < nodes.Count; i++)
{
if (!(i == (nodes.Count - 1)))
{
for (int j = nodes.Count; j > i; j--)
{
if (!(j == (nodes.Count)))
{
if (nodes[i].Title.CompareTo(nodes[j].Title) > 0)
{
nodes[i].Move(nodes, nodes[j]);
web.Update();
}
}
}
}
}
}
catch (Exception _e)
{
textBox1.Clear();
textBox1.Text += "Error occured in navigation sorting process" + System.Environment.NewLine +
System.Environment.NewLine + "Error: " + _e.Message + System.Environment.NewLine +
System.Environment.NewLine + "Stack Trace: " + _e.StackTrace + System.Environment.NewLine;
}
}
}
}
}
}
Finally, a piece of advise when working with SharePoint – Don’t forget the basics!
Comments
- Anonymous
June 07, 2008
Modifying navigation elements through code is quite tricky in WSS 3.0. Here are few scenarios, I’ve tried