Code to Browse a Catalog

This section contains code examples for some of the functions needed to implement browsing. These functions are to find categories in a catalog, to find products in a category, to find product properties, and to display the results. Parameter validation and error handling are not considered in these examples. A more extensive example can be found in the Browse.asp file in the Catalog sitelet, which is in the Commerce Server 2002 Software Development Kit (SDK). The Browse.asp file and its included files demonstrate parameter validation, error handling, and generation of links from recordsets.

The following steps show how to implement browsing:

  1. Get the CatalogManager object and the catalog.

    The CatalogManager object should already be created and initialized in the Global.asa file. Get a local reference to the CatalogManager object and the catalog, as shown in the following code:

    Dim oCatMgr
    Dim oCatalog
    Set oCatMgr = Application("MSCSCatalogManager")
    Set oCatalog = oCatMgr.GetCatalog("Adventure Works Catalog")
    
  2. Set the catalog language.

    The ProductCatalog object has a language property that allows the user to set the language in which the catalog data is to be displayed. Get a local reference to the ProductCatalog object and set a language for the catalog, as shown in the following code:

    oCatalog.language = "en-us"
    
  3. Get the categories.

    The categories in a catalog are in a hierarchy. The root categories may or may not have child categories, but they are at the base of the hierarchy. The following example creates a recordset of the root categories:

    Dim rsCategories
    Set rsCategories = oCatalog.RootCategories
    

    The following example shows how the category hierarchy can be traversed. Given a recordset of categories at some level in the hierarchy, the CategoryName field is used to retrieve a recordset of the child categories of the active row in the given recordset:

    Dim strName
    ' Return the name of the category in the active row of
    ' the recordset (this would usually be inside a loop).
    strName = rsCategories("CategoryName")
    Set oCategory = oCatalog.GetCategory(strName)
    

    After you have the category object, obtaining the child categories is straightforward:

    'oCategory is an existing Categoryobject.
    Set rsChildCategories = oCategory.ChildCategories
    
  4. Get the products.

    The following example assumes that all products are in categories. Root products, that is, products that are not in any category, can exist; however, the usual design calls for all products to belong to a category. Having all products within categories simplifies code and product management. The following code retrieves the products in the "shoes" category:

    Dim rsProducts
    Set rsProducts = oCatalog.GetCategory("Boots").Products
    
  5. Get the details about the product.

    When displaying a product, you will typically want to display some details about that product. The following example creates a recordset of the properties of a specified product:

    ' vProductID is the unique identifier for a product. This example 
    ' uses the ProductCode as the product ID.
    Dim rsProductDetail
    Set rsProductDetail = _
        oCatalog.GetProduct("AW099-15").GetProductProperties
    
  6. Display the results.

    The following code example demonstrates how to display a set of products contained in a recordset called rsProducts.

    <br>
    <%
    ' Loop through the recordset of products and
    ' write out the properties of the products.
    ' The constants are field names.
    Do While Not rsProducts.EOF
    
        Response.Write "Name: " & _
                       rsProducts("Name") & "<BR>"
        Response.Write "Product Code: " & _
                       rsProducts("ProductID") & "<BR>"
        Response.Write "Description: " & _
                       rsProducts("Description") & "<BR>"
    
        rsProducts.MoveNext()
    Loop
    %>
    

Copyright © 2005 Microsoft Corporation.
All rights reserved.