Condividi tramite


How to Get Categories

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

The categories in a catalog are in a hierarchy. The root category is the base of the hierarchy. The next level in the hierarchy is child categories of the root category. These child categories may or may not have their own child categories.

To get all the categories in a catalog, first get the root category. Then get the child categories.

You can also get a specific category and use a CategoryConfiguration object to specify the search options.

To get all the categories in a product catalog

  1. Get the root category. To do this, use one of the GetRootCategory methods on the ProductCatalog object.

  2. Get the child categories of the root category. To do this, use the ChildCategories property on the Category object. This property returns a dataset of all categories under the root category.

  3. Iterate through the categories and display the information.

  4. To get a specific category, use one of the GetCategory methods on the ProductCatalog object.

  5. To specify search options, create a CategoryConfiguration object for the config parameter.

Example

This code example demonstrates how to get the categories of a product catalog. It first gets the catalog and then gets the root category. The example then gets a collection that contains the child categories of the root category and iterates through the dataset to access each category. It writes the name of each category to the console. The example then creates a CategoryConfiguration object that specifies search criteria.

It then gets the category specified by parameter categoryName from the catalog using the search criteria.

private static void GetCatalogCategories(CatalogContext context, string catalogName, string categoryName)
{
    // Get the catalog with the name specified in catalogName. 
    // Return the catalog data in English, as specified by "en-US");
    ProductCatalog productCatalog = ProductCatalog)context.GetCatalog(catalogName, "en-US");
    Console.WriteLine(productCatalog.Name);
    // Get the root category.
    Category rootCategory = productCatalog.GetRootCategory();
    CatalogItemsDataSet categories = rootCategory.ChildCategories.DataSet;
    // Iterate through the categories and display the category name.
    foreach (CatalogItemsDataSet.CatalogItem category in categories.CatalogItems)
    {
        Console.WriteLine(category.DisplayName);
    }

    //  Get a specific category from the catalog.
    // First create a CategoryConfiguration object that contains the search criteria.
    CategoryConfiguration categoryConfiguration = new CategoryConfiguration();
    categoryConfiguration.LoadChildProducts = true;
    categoryConfiguration.ChildProducts.SearchOptions = new CatalogSearchOptions();
    categoryConfiguration.ChildProducts.SearchOptions.ClassTypes = CatalogClassTypes.ProductClass | CatalogClassTypes.ProductFamilyClass;
    categoryConfiguration.ChildProducts.SearchOptions.SetPaging(1, 20);

    // Now get the category from the catalog using the specified search options.
    Category sbCategory = catalog.GetCategory(categoryName, categoryConfiguration);
}

See Also

Other Resources

Managing Products and Categories by Using the Catalog API