Sorting Profile in Commerce Server 2007 and Mojave

on Friday, December 19, 2008

At the time of this writing sorting is not supported by Mojave on the Profile entity. As a matter of fact, if you write and execute a Mojave query, the sortProperties attribute will only have any effect if you are dealing with one of the following entities:

  • Catalog
  • CatalogEntity
  • Category
  • Product
  • Variant
The CommerceSortProperty will simply be ignored for any other entities including Profile.

So how exactly do you sort one of these entities? Well, not surprisingly, you will have to do it yourself. For example, if you run a query to get the credit cards from the user profile, the returned list will be random at best. Typically, you will store this result inside a Collection object of some sort. The DefaultSite that ships with CTP4, uses a generic Collection object called Collection. Before you return this unsorted list to your caller, you can send this object to the following method which will sort it for you.

private static ICollection SortCreditCardsCollectionByDateCreated(Collection creditCardsCollection)
{
List listCreditCards = new List(creditCardsCollection);
listCreditCards.Sort(
delegate(CreditCard x, CreditCard y)
{ return DateTime.Compare(x.DateCreated, y.DateCreated); }
);
return (new Collection(listCreditCards));
}

The above method sorts the list on the DateCreated attribute. You can take it one step further and generalize this method to actually pass in the attribute on which you wish to sort, and place it in a MojaveUtilities class if you wish.

And now, you can sort profile or any other entity you wish to sort in Commerce Server 2007 using Mojave API.

1 comments:

maria said...

Visit this amazingly helpful website for real estates!!
Real estate