In this tutorial, you will learn how to query customers stored in Episerver commerce in code. The easiest way to get a user is to use the GetContactForUser method and pass in a MembershipUser. You can call Membership.GetUser(username) to get the user and then you simply pass the CustomerContext to get the user, as can be seen below:

Getting a user when you can't use Membership User

Episerver has also provided another way to access a commerce customer, however, it has a hidden gotcha. There is an overload parameter to GetContactByUserId which takes a username for type string. Unfortunately, this call never returned any data. I looked within my commerce database, in cls_Contact table. As shown below, you can see the word "String:" is appended in front of the username when it is stored:

Commerce_Customer_Table_Error

If you combine the value String: with the username you want to search for (and add valid CustomerContact) the API should now magically work 🔮


Two ways to search for customers. Happy Coding 🤘