Feeds:
Posts
Comments

Archive for November, 2011

As part of a Sharepoint solution, I had the need to remove the default Quick Launch entries (Lists, Libraries, Discussions), and insert my own sections with links to specific lists and pages.

The following method gets it done:

public static void CreateSideNav(string siteName, string webName)
{
	using (var spSite = new SPSite(siteName))
	{
		using (SPWeb spWeb = spSite.OpenWeb(webName))
		{
			try
			{
				SPNavigationNodeCollection leftNav = spWeb.Navigation.QuickLaunch;
				var nodesToDelete = new List<int>();

				for (int i = 0; i < leftNav.Count; i++)
				{
					if (leftNav[i] != null)
					{
						SPNavigationNode leftNavNode = leftNav[i];
						if (DefaultQuickLaunchEntriesToDelete.Contains(leftNavNode.Title))
							nodesToDelete.Add(i);
					}
				}

				nodesToDelete.Sort();
				nodesToDelete.Reverse();
				nodesToDelete.ForEach(x => leftNav[x].Delete());

				spWeb.Update();

				leftNav = spWeb.Navigation.QuickLaunch;

				// Add section headings
				var reportsNode = new SPNavigationNode("Reports", spWeb.Url, true);
				leftNav.AddAsFirst(reportsNode);

				var dataListsNode = new SPNavigationNode("Data Lists", spWeb.Url, true);
				leftNav.AddAsFirst(dataListsNode);

				var mainListsNode = new SPNavigationNode("Primary Lists", spWeb.Url, true);
				leftNav.AddAsFirst(mainListsNode);


				// Add individual links
				SPNavigationNode node;

				// MainListsNode
				node = new SPNavigationNode("Committees", string.Format("{0}/Lists/Committees", spWeb.Url), true);
				mainListsNode.Children.AddAsLast(node);

				node = new SPNavigationNode("Members", string.Format("{0}/Lists/Members", spWeb.Url), true);
				mainListsNode.Children.AddAsLast(node);

				node = new SPNavigationNode("Services", string.Format("{0}/Lists/Services", spWeb.Url), true);
				mainListsNode.Children.AddAsLast(node);

				// DataListsNode
				node = new SPNavigationNode("Committee Types", string.Format("{0}/Lists/CommitteeTypes", spWeb.Url), true);
				dataListsNode.Children.AddAsLast(node);

				node = new SPNavigationNode("Jurisdictions", string.Format("{0}/Lists/Jurisdictions", spWeb.Url), true);
				dataListsNode.Children.AddAsLast(node);

				spWeb.Update();
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.ToString());
			}
		}
	}
}

I then reference this from a feature receiver in my package, and pass in the specific site and web names. I also define a List of Quick Launch entries to remove like so:

static List<string> DefaultQuickLaunchEntriesToDelete { get { return new List<string>() { "Lists", "Libraries", "Discussions" }; } }

With this we will, from the specified web, remove those default entries (of course you can specify any you wish) and insert our own.

Read Full Post »