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 2000 Software Development Kit (SDK). The Browse.asp file and its included files demonstrate parameter validation, error handling, and generation of links from recordsets.

  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.

    Dim oCatMgr
    Dim oCatalog
    Set oCatMgr = Application("MSCSCatalogManager")
    Set oCatalog = oCatMgr.GetCatalog("ChildrensClothing")
    
  2. 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
    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 rsChildCategories = oCatalog.GetCategory(strName).ChildCategories
    
    If you have the category object in hand obtaining the child categories is straightforward:
    'oCategory is an existing Category object.
    Set rsChildCategories = oCategory.ChildCategories
    
  3. 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 "shoe" category:

    Dim rsProducts
    Set rsProducts = oCatalog.GetCategory("Shoes").Products
    
  4. Get product detail.

    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. The Catalog sitelet uses the ISBN as the product ID.
    Dim rsProductDetail
    Set rsProductDetail = _
        oCatalog.GetProduct(0-7356-0513-0).GetProductProperties
    
  5. Display the results.

    The following code example demonstrates how to display a set of products contained in a recordset called rsProducts. For an example of generating links from a recordset, see the Browse.asp file in the Catalog Sitelet in the Commerce Server 2000 SDK.

    <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 "ISBN Number: " & _
                       rsProducts("isbn") & "<BR>"
        Response.Write "Title: " & _
                       rsProducts("name") & "<BR>"
        Response.Write "Author: " & _
                       rsProducts("author") & "<BR>"
        Response.Write "Published in: " & _
                       rsProducts("Publication Year") & "<BR>"
    
        rsProducts.MoveNext()
    Loop
    %>
    


All rights reserved.