Code to Perform a Search of a Catalog

The following code example shows a wrapper function that performs a search of a catalog. The code uses typical values for return properties and sorting properties, and simplifies performing a search (both free-text search and SQL WHERE clause) by building the parameters for a call to the CatalogManager.Search method.

The constants used to build the values of the sPropertiesToReturn and sPropertiesToSortOn variables will need to be defined according to your catalog definition.

When you use this code, note the following requirements:

  • Ensure that the properties listed in the example (in sPropertiesToReturn) exist in the catalogs you are searching.
  • Ensure the catalogs listed in the example (in sCatalogsToSearch) exist in the Product Catalog System.

Ensure that the sort of properties listed in the example (in sPropertiesToSortOn) exist in the catalog(s) you are searching in.

' Function: rsSearch
'
' Description:
'    Performs a search using typical values.
'    Returns the results as a recordset.
'    iRecordCount gets set to the number of records retrieved.
'
' Parameters:
'    sSQLWhereClause     - the SQL Where Clause to filter against.
'                          Example, "cy_list_price < 100"
'    sFTSPhrase          - the free text string to find.
'                          Example, "tent"
'    iStartingRecord     - return results starting from
'                          this record number.
'    iRecordsToRetrieve  - return maximum this many records.
'    iRecordCount        - return the actually retrieved number
'                          of records in this parameter.
'    sLanguage          - the language to search and results in.
'                          Example, "en-us"
'
' Returns:
'   Recordset with search results.
' -----------------------------------------------------------------------
Function rsSearch(ByVal sSQLWhereClause, _
                          ByVal sFTSPhrase, _
                          ByVal iStartingRecord, _
                          ByVal iRecordsToRetrieve, _
                          ByVal sLanguage, _
                          ByRef iRecordCount)

    ' Declare variables:
    Dim sPropertiesToReturn
    Dim sPropertiesToSortOn
    Dim rsResult
    Dim iClassType
    Dim sCatalogsToSearch

    ' Build the parameter strings to select 
    ' properties to return and to control sorting:
    ' "name" and "description" are required product 
    ' properties and cannot have null values.
    sPropertiesToReturn = PRODUCT_NAME_PROPERTY_NAME & "," & _
                          PRODUCT_DESCRIPTION_PROPERTY_NAME & "," & _
                          CATALOG_NAME_PROPERTY_NAME & "," & _
                          CATEGORY_NAME_PROPERTY_NAME & "," & _
                          CATALOG_PRODUCTID_PROPERTY_NAME & "," & _
                          CATALOG_VARIANTID_PROPERTY_NAME & "," & _
                          DEFINITION_TYPE_PROPERTY_NAME

    ' "name" is a required product property and cannot have a null value.
    sPropertiesToSortOn = CATEGORY_NAME_PROPERTY_NAME & "," & _
                         PRODUCT_NAME_PROPERTY_NAME

    ' Determine what catalogs to search.
    ' This example assumes that you will always search two catalogs:
    ' CatalogA and CatalogB. You could get this parameter by having
    ' the user select which catalogs to search, or from a function
    ' that selected catalogs based on a user profile.
    sCatalogsToSearch = "CatalogA, CatalogB"

    ' Determine the type of data wanted. This example will return
    ' categories, products, and product families.
    iClassType = cscProductFamilyForVariantsClass Or _
                 cscProductClass Or 
                 cscCategoryClass

    ' Call the Search method and return the recordset.
    Set rsSearch = _
          MSCSCatalogManager. Search( _
             sSQLWhereClause, _
             sFTSPhrase, _
             sCatalogsToSearch, _
             iClassType, _
             sPropertiesToReturn, _
             sPropertiesToSortOn, _
             True, _
             iStartingRecord, _
             iRecordsToRetrieve, _
             sLanguage, _
             iRecordCount _
             )

End Function

Copyright © 2005 Microsoft Corporation.
All rights reserved.