Documentation Center

Generating dynamic navigation

Typically, the navigational structure of your Web site corresponds to the hierarchy of the Structure Groups in your Content Manager Publication. You can generate a set of navigation controls on a Web page dynamically by accessing the Structure Groups.

The following code retrieves an array of top-level Structure Groups that you can turn into an XHTML fragment. In a Dreamweaver template, you can include this fragment on your page as a Component Presentation.

One important consideration is the order in which you want to display the navigation controls. Structure Groups in Content Manager are ordered alphabetically, so you must either reorder them on the basis of a metadata field that contains a number to indicate the position of the Structure Group, or name the Structure Group in such a way that they are correctly sorted already (for example, 01_Corporate, 02_News, and so on). This code samples presupposes that a metadata field called "Position" indicates order.

// Method that retrieves the top-level Structure Groups of a Publication, sorted.
private List<RepositoryLocalObject>

GetSortedTopLevelStructureGroups(Publication publication)

{
	// Get the root Structure Group of a Publication object
	StructureGroup rootStructureGroup = publication.RootStructureGroup;

	// Create a filter to only retrieve Structure Group objects.
	Filter filter = new Filter();
	filter.Conditions["ItemType"] = ItemType.StructureGroup;

	// Use the filter to get the Structure Groups contained in this root Structure Group.
	IList<RepositoryLocalObject> structureGroups = rootStructureGroup.GetItems(filter);

	// Convert the result to a type that can be sorted, and perform a sort.
	List<RepositoryLocalObject> sortableStructureGroups =
		new List<RepositoryLocalObject>(structureGroups);

	// Sort this list using a custom compare class (see below)
	sortableStructureGroups.Sort(new StructureGroupComparer());

	// Return the List of sorted Structure Groups
	return sortableStructureGroups;
}


// Compare class to compare two Structure Groups to determine which one comes first.
private class StructureGroupComparer: IComparer<RepositoryLocalObject>

{
	// A cache to improve sort performance.
	private IDictionary<RepositoryLocalObject, double> _cache =
		new Dictionary<RepositoryLocalObject, double>();

	// The compare method which returns the result
	// of comparing the positions of the Structure Groups.
	public int Compare(RepositoryLocalObject structureGroup1,
		RepositoryLocalObject structureGroup2)
	{
		return GetPosition(structureGroup1).CompareTo(GetPosition(structureGroup2));
	}

	private double GetPosition(RepositoryLocalObject structureGroup)
	{
		// Check the cache to see if you already retrieved
		// the Position of this Structure Group earlier.
		if (!_cache.ContainsKey(structureGroup))
		{
			// If it is not in the cache, retrieve
			// the metadata fields of this Structure Group.
			ItemFields itemFields =
				new ItemFields(structureGroup.Metadata,
					structureGroup.MetadataSchema);
			// Find out the position.
			double position = ((NumberField) itemFields["Position"]).Value;
			// Add the position to the cache
			_cache[structureGroup] = position;
		}

		// Return the position of this Structure Group.
		return _cache[structureGroup];
	}

}

You now have a sorted list of the top-level Structure Groups in a Publication. Assuming that each Web site section has a 'front page' called, say, index.html, and that the names of your Web folders correspond to those of the Structure Groups, you can now turn this sorted list of objects into an XHTML fragment. To optimize performance, it makes sense to use this C# code as a Template Building Block in a Compound Component Template. By putting the XHTML fragment in the Output item of that Template's package, you can create a standalone Dynamic Component Presentation that every Web page can include. This means that if the list of top-level Structure Groups changes, you only need to republish the Dynamic Component Presentation, rather than republish every Page that contains the navigation controls.