Create a Search Function

Enable free text searches on a 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.
    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;
    }