Commerce Server Blog

Build world class ecommerce applications using Microsoft Commerce Server
  •  
  • Home
  • About
  • Advertise
  • Copyright Policy
  • Disclaimer

Catalog Search – Ascending on first property and descending on second property

ravikanth | October 21, 2009

Today I saw a forum post stating how can we achieve catalog search which has different sorting options on different properties, i.e., the user wants to have ascending order on one property and descending on second property at one shot. At the beginning I thought commerce server doesn’t support this feature but it does support with searchOptions (thanks for Gael). Commerce Server provides SearchOptions by which we can get search narrow down results (i.e, we can select the properties you want to return, number of rows, sort either descending or ascending, etc) in a single call.

Through SortProperty we can pass the properties name and order and make sure you keep property in [] braces. If you miss braces then you will receive “Search Failed” error. Here is the example where I am trying to fetch all products where the list price is greater than 10$ and the sorting on category name (desc), productId (asc) and variantId (desc). 

CatalogContext context = CommerceContext.Current.CatalogSystem;
// Search the catalog.
// Create the search options.
CatalogSearchOptions searchOptions = new CatalogSearchOptions();
searchOptions.PropertiesToReturn = "CategoryName, DefinitionName, i_classtype, ProductId, VariantId, DisplayName";
searchOptions.SortProperty = "[CategoryName]DESC, [ProductId]ASC, [VariantId]DESC";
CatalogSearch catalogSearch = context.GetCatalogSearch();

// Specify the catalogs to search.
// This is a comma-separated list of catalogs to search,
// for example, "Catalog1,catalog2".
catalogSearch.CatalogNames = "Adventure Works Catalog";

// Return all items with price greater than $10.00.
catalogSearch.SqlWhereClause = "cy_list_price>10";

catalogSearch.SearchOptions = searchOptions;

// Perform the search.
int totalRecords = 0;
CatalogItemsDataSet catalogItems = catalogSearch.Search(out totalRecords);
results.DataSource = catalogItems.Tables[0];
results.DataBind();

Categories
commerce server 2007
Comments rss
Comments rss
Trackback
Trackback

« Troubleshoot: Code 3005 in Profiles.ProfileContext Tasks to view Commerce Server Reports »

2 Responses to “Catalog Search – Ascending on first property and descending on second property”

  1. Catalin says:
    December 7, 2009 at 9:38 am

    Hello,

    Very interesting post!
    However, how does this kind of sorting take place? If first sorts the results based on the first criteria, and then, for those results
    which are equal it applies the second criteria?

    And a second question:
    How do you obtain the search results based only on free text search ranking (without any other sorting option)
    in CS 2009 extensibility kit (Microsoft.Commerce.Portal.SearchController.cs, public static List DoProductQuerySearch

    Thank you in advance

    Reply
  2. Catalin says:
    December 8, 2009 at 3:15 am

    I solved it!
    For anyone interested to know.
    FreeTextSearch_Rank is the property you need to add to the web part’s sort options.
    You also need to add it in MetadataDefinitions.xml, among the properties.

    Reply

Leave a Reply

Click here to cancel reply.

Powered by WP Hashcash

Sign up for Newsletter


Categories

Quiz

  • Catalog Quiz for Beginners
  • Profile Quiz for Beginners
  • Quiz 1 for Advance Users
  • Quiz for Beginners

Recent Posts

  • Account Creation Approval Process
  • Troubleshoot: Unauthorized exception when the new user tries to access MyAccount page
  • Microsoft Commerce Server 2009 code name "R2" – January 2010 Community Technical Preview
  • Merchant Access to Catalog Manager
  • Commerce Server 2009 December VPC ready for download
  • Will Commerce Server November CTP work on SharePoint 2010
  • Show Silverlight Advertisements in Commerce Server
  • How Commerce Server Calculates Inventory
  • Targeting on Other profile Objects
  • Commerce Server 2009 Book

Archives

  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008

Tags

add discounts add images to profile Advantages of commerce server approve discounts authentication auto login process automatic user creation basket Brand Management business tools campaign management Catalog catalog import Category CategoryConfiguration cc_number checkout commerce 7 commerce server commerce server 2002 commerce server 2007 commerce server 2007 Display Types Commerce Server SP2 discounts ecommerce Estimation inventory Kanth Koppala marketing system migration error Mojave Product profile profile subsystem property metadata purchase order quick checkout Ravi Ravi Kanth site cache refresh starter site tools transactions error Variant
(c) 2009 Commerce Server Blog. All rights Reserved. Articles cannot be reproduced without permission from the author.Write to me at kanth@ravikanth.net if you have any comments, questions, suggestions about this site or would like to send us a tip
About Us | Terms of Use | Disclaimer | Advertise .