PerformSearch

PerformSearch

The annotated version of the routine PerformSearch, found in the file SearchResults.aspx.vb, follows:

Protected Sub PerformSearch(ByVal searchCatalog As String, _                             ByVal searchKeywords As String, _                             Optional ByVal displayNo As Integer = -1) 

Define objects of class DataSet, class CatalogSearch, and class CatalogSearchOptions (the latter two classes are in the Commerce Server 2002 Base Class Library (BCL) for catalogs, accessed using the namespace Microsoft.CommerceServer.Runtime.Catalog.

    Dim data As DataSet     Dim displaySearchResults As Boolean = True     Dim catalogSearch As Microsoft.CommerceServer.Runtime.Catalog.CatalogSearch = _             New Microsoft.CommerceServer.Runtime.Catalog.CatalogSearch()     Dim searchOptions As CatalogSearchOptions = New CatalogSearchOptions() 

Set the ClassTypes property of the CatalogSearchOptions object to include contain the indicated search targets using the CatalogClassTypes enumeration, available using the namespace Microsoft.CommerceServer.Runtime.Catalog.

    ' Set the class type to get Products, Families and Families for variants     searchOptions.ClassTypes = CatalogClassTypes.ProductClass Or _                                CatalogClassTypes.ProductFamilyClass Or _                                CatalogClassTypes.ProductFamilyForVariantsClass 

Assign the passed keywords and SearchOptions object to the corresponding properties in the CatalogSearch object, and set the record count in the SearchOptions object to the passed value.

    catalogSearch.FreeTextSearchPhrase = searchKeywords     catalogSearch.SearchOptions = searchOptions     catalogSearch.SearchOptions.RecordsToRetrieve = displayNo     Try         ' if "all catalogs" is selected         If searchCatalog = CommerceApplication.ResourceString(ResID.allCatalogs) Then 

If the search request is for all catalogs, retrieve a string collection of all catalogs and assign it to the CatalogNamesCollection property of the CatalogSearch object. Also set the properties to be returned as established in the constant SEARCH_RETURNPROPERTIES. This constant contains a string that specifies four required catalog properties to be returned. Otherwise, an error could occur when the different catalogs have different properties defined.

            Dim catalogCollection As ReadOnlyStringCollection             catalogCollection = GetCatalogs()             If (Not catalogCollection Is Nothing) _                    AndAlso (catalogCollection.Count > 0) Then                 catalogSearch.CatalogNamesCollection = catalogCollection                 searchOptions.PropertiesToReturn = SEARCH_RETURNPROPERTIES 

Perform the actual search using the Search method of the CatalogSearch object.

                data = catalogSearch.Search()             End If         Else 

Instead of using the CatalogNamesCollection property of the CatalogSearch object to specify which catalogs are to be searched, use the CatalogNames property, assigning the passed catalog name to it (although the CatalogNames property can handle multiple catalogs using a comma-separated list of catalog names, the user interface of the International Retail Site only permits a choice between all catalogs and a particular, single catalog). Note that it could improve performance if this search was also set up to only return the properties specified in the constant SEARCH_RETURNPROPERTIES, since those are the only properties displayed. This optimization is left as an exercise for the reader.

            catalogSearch.CatalogNames = searchCatalog 

Perform the actual search using the Search method of the CatalogSearch object.

            data = catalogSearch.Search()         End If 

Check for exceptions thrown during the search.

    Catch Ex As Exception         data = Nothing     End Try 

If no results are returned, record that no results should be displayed using the displaySearchResults variable.

    'If the Dataset returned by Search is empty      ' then do not display search results     If data Is Nothing OrElse data.Tables(0).Rows.Count = 0 Then         displaySearchResults = False     End If 
Previous  Next


Microsoft Corporation - Microsoft. Net Server Solutions for the Enterprise
Microsoft .NET Server Solutions for the Enterprise
ISBN: 0735615691
EAN: 2147483647
Year: 2002
Pages: 483

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net