Share via


How to Create a Category Hierarchy

For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.

You create catalog hierarchies to organize the products in the catalog to make it easier for customers to navigate to the products they want to buy.

For example, if you have a large catalog, you can create a parent category that includes several other categories, known as child categories. When customers navigate to the parent category, the child categories appear, enabling customers to navigate quickly to the category that contains the products they want.

Each category and product can have one primary parent category. The primary parent category provides a canonical path to a child category or product.

You should be careful to not add circular relationships. That is, if category2 is the child of category1, it cannot also be the parent of category1.

To create a category hierarchy

  1. Use the AddChildCategory method on the StaticCategory object to add an existing category as the child of the current category.

  2. Use the AddParentCategory method on the CatalogItem object to add an existing category as the parent of the current category.

  3. Use the RemoveParentCategory method on the CatalogItem object to remove the parent category from the current category. This method moves the current category up one level in the hierarchy and does not remove the category from the catalog.

  4. Use the RemoveChildCategory method on the StaticCategory object to remove a child category from the current category. If the removed child category has child categories, they are moved up one level in the hierarchy. The child category is not removed from the catalog.

  5. Use the ChildCategories property on the Category object to get all child categories of the current category. This property returns a collection of all child categories in the current category.

  6. Use the ParentCategories property on the CatalogItem object to get all parent categories of the current category. This property returns a collection of the parent categories for the current category.

Example

This example demonstrates how to create a category hierarchy. It adds a parent category and a child category to the current category. It then removes the parent category from the hierarchy.

public static void CreateHierarchy(CatalogContext context, string catalogName, string categoryName, string parentCategory, string childCategory)
{
    // Create a hierarchy with the existing categories in a catalog.
    // Get the catalog.
    ProductCatalog productCatalog = context.GetCatalog(catalogName);
    
    // Get the "current" category.
    StaticCategory currentCategory = (StaticCategory)productCatalog.GetCategory(categoryName);
    
    // Add an existing category as the parent of the current category.
    currentCategory.AddParentCategory(parentCategory);

    // Add an existing category as the child of the current category.
    currentCategory.AddChildCategory(childCategory);

    // Save the changes.
    currentCategory.Save();

    // Remove the parent from the current category and save the changes.
    currentCategory.RemoveParentCategory(parentCategory);
    currentCategory.Save();
}

See Also

Other Resources

How to Create a Category Relationship

How to Browse a Catalog