Share via


How to Add a Category

You can add a category to a base catalog or a virtual catalog by using one of the CreateCategory methods.

You can use the APIs to create category hierarchies. Creating category hierarchies helps organize products in your catalogs and makes navigation on your Web site easier for your users.

You can create a category that has parent categories and/or child categories.

For example, the category CDs can have the child categories Rock, Rhythm and Blues, and Classical. The child categories can also contain child categories of their own. For example, the category Rock can contain the child categories Pop and Alternative.

Each category and product can have one primary parent category. The primary parent category provides a canonical path to a child category or product. In addition, if a product is in more than one category, and pricing is set at the category level, the price of the primary parent category overrides the price in all other categories that are using category pricing.

To add a category to a base catalog

  1. Get the catalog to which you want to add the category.

  2. Use the CreateCategory method to add the category to the catalog.

Example

This example adds a category "Autobiography" to an existing catalog named "Books." In this example the category definition already exists.

public static void CreateNewCategory(CatalogContext context, string definitionName)
{
    // Get the catalog.
    BaseCatalog baseCatalog = (BaseCatalog)catalogContext.GetCatalog("Books");
    try
    {
        // Create the new category in the catalog and save.
        Category categoryAutobiography = baseCatalog.CreateCategory(definitionName, "Autobiography");
        categoryAutobiography.DisplayName = "Autobiography";
        categoryAutobiography.Save();
    }
    catch (EntityAlreadyExistsException e)
    {
        Console.WriteLine(string.Format("Exception: {0}", e.Message));
    }
}

See Also

Other Resources

Managing Base Catalogs by Using the Catalog API