Create a Search Function

Current version: 8.2

Enable free text searches on a Sitecore Commerce 8.2 powered by Commerce Server and Commerce Server 11.4 website.

Complete the following prerequisites:

At your visual studio solution setup

  1. Use SearchHelpers.GetFacetFieldsForItem to retrieve facets for this page.

  2. Use SearchHelpers.GetSortFieldsForItem to get any specific sort fields that are expected to be used on this page.

  3. Use SearchHelpers.GetFacetFieldsForItem to retrieve facets for this page.

  4. Create an instance of the CommerceSearchOptions class. Use SearchNavigation.GetCatalogItem to actually search for the term, you need to pass it a search term and an instance of the options class.

    RequestResponse
    var searchManager = CommerceTypeLoader.CreateInstance<ICommerceSearchManager>();
    var requiredFacets = searchManager.GetFacetFieldsForItem(this.Item);
    var sortFields = searchManager.GetSortFieldsForItem(this.Item);
    var searchKeyword = "boots";
    var catalogName = "Adventure Works Catalog";
    var currentPageNumber = 0;
    
    var productSearchOptions = new CommerceSearchOptions(Constants.Settings.DefaultItemsPerPage, currentPageNumber);
    
    var searchResponse = SearchNavigation.SearchCatalogItemsByKeyword(searchKeyword, catalogName, searchOptions);
    if (searchResponse != null)
    {
        var foundItems = searchResponse.ResponseItems;
        var totalProductCount = searchResponse.TotalItemCount;
        var totalPageCount = searchResponse.TotalPageCount;
        var facets = searchResponse.Facets;
    }

Do you have some feedback for us?

If you have suggestions for improving this article,