How To Search For Users In Episerver Commerce

A key aspect of many websites is building an on-line community. In today's guide, I'm going to quickly cover how to get your customer information from Episerver commerce.

Getting a user with the Membership User

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 this into the CustomerContext to get the user.

            var user = Membership.GetUser(username);

            var customerContext = new Mediachase.Commerce.Customers.CustomerContext();
            var contact = customerContext.GetContactForUser(user);

Getting a user when you can't use Membership User

Episerver has also provided another way to access a commerce customer.  I've had a few times when I've been searching for users in an admin page when I can't create a MembershipUser. However, there is a slight issue within.  I tried calling the overload of GetContactByUserId which takes a string username but, unfortunately, it never returned anything.  I looked at the commerce database in the cls_Contact table, as shown below and you can see the word "String:" is appended in-front of the username. Commerce_Customer_Table_Error If you pass in the value "String:" as your username, it will return you a valid CustomerContact

var customerContext = new Mediachase.Commerce.Customers.CustomerContext();
var contact = customerContext.GetContactByUserId(string.Format("String:{0}", "username"));

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top