Last week I come across a requirement to get Episerver commerce list of customers based on filtered criteria such a get customers based on the validity of a certain field. As we all know Episerver store customer data in Business Foundation class called “Contact”.

In my case, I have to get the list of customers opted-in to receive a newsletter. I already have a custom field in the “Contact” BF class called “Subscribed”. The data type of this field is boolean.

The first step is to create filter criteria of type “FilterElement”

var SubscribedFilter = new FilterElement(StringConstants.CustomFields.Subscribe, FilterElementType.Equal, true);
var filters = new[] { SubscribedFilter };

We can create multiple filters and add them together separated by comma such as

var SECONDFILTER = new FilterElement("Subscribe", FilterElementType.Equal, true);
var filters = new[] { SubscribedFilter, SECONDFILTER };

Next step is to call BusinessManager and get results

var customers = BusinessManager.List("Contact", filters).Cast<CustomerContact>();

The last part “Cast<CustomerContact>()” is optional. You can use it if you want to return strongly typed results.

Categorized in:

Tagged in: