One of the challenges I have run into working with Commerce Server is utilizing the multi-value key fields in the profile system. I am finding that they are a bit clunky. It would be nice if the Commerce Server team would implement some additional functionality to make simple procedures like adding an address to a user profile easier. Using the UserObject profile as an example, the way it currently works is that if you add an Address profile and want to link it to a specific user, you need to add the AddressID (a GUID) to the address_list property in the UserObject profile. This is stored in the database as a string field and just delimits the GUIDs with a semi-colon. When you retrieve the address_list property from the profile system, it returns you an object array of the GUIDs. You can then just manipulate the array to add a new value or iterate through it to retrieve the appropriate address profiles. The problem is that I found very little documentation regarding the use of these fields. I have included the method I am using below to add a new address to the User profile. I’m sure there are better ways to do this and if anyone can point me in the right direction I would be thrilled.
public static void InsertIntoUserAddressList(AddressInfo newAddress, string userEmail)
{
Profile user = CommerceContext.Current.ProfileSystem.GetProfile("GeneralInfo.email_address", userEmail, "UserObject");
ArrayList addresses = new ArrayList();
if (user.Properties["GeneralInfo.address_list"].Value != null)
{
object[] addr = (object[])user.Properties["GeneralInfo.address_list"].Value;
foreach(object objAddress in addr)
{
addresses.Add(objAddress);
}
}
addresses.Add(newAddress.AddressID.ToString());
user.Properties["GeneralInfo.address_list"].Value = addresses.ToArray();
user.Update();
}