-1

I'm trying to set & get the custom property values for a commerce user.
While fetching the value, there is an error:

The given key was not present in the dictionary

public void SetCommerceUserProfilePropertyValues(string usernameWithDomain, List<KeyValuePair<string, object>> keyValuePairs)
        {
            if (keyValuePairs != null && keyValuePairs.Count > 0)
            {
                Sitecore.Commerce.Entities.Customers.CommerceUser commerceUser = GetCommerceUser(usernameWithDomain);

                if (commerceUser != null)
                {
                    foreach (KeyValuePair<string, object> kvp in keyValuePairs)
                    {
                        commerceUser.SetPropertyValue(kvp.Key, kvp.Value);
                    }
                }
            }
        }

public object GetCommerceUserProfilePropertyValue(string usernameWithDomain, string propertyName)
        {
            CommerceUser commerceUser = GetCommerceUser(usernameWithDomain);
            return commerceUser != null ? commerceUser.GetPropertyValue(propertyName) : null;
        }

public void MyMethod()
{
    string usernameWithDomain = "Storefront\\myname@email.com";  
    List<KeyValuePair<string, object>> keyValuePairs = new List<KeyValuePair<string, object>> {
                        new KeyValuePair<string, object>("ForgotPasswordToken", Guid.NewGuid()),
                        new KeyValuePair<string, object>("ForgotPasswordTokenCreatedDate", DateTime.Now)
                    };
    
    ///save custom property values
    SetCommerceUserProfilePropertyValues(usernameWithDomain, keyValuePairs); 
    
    --Exception here
    var xtoken = GetCommerceUserProfilePropertyValue(usernameWithDomain, "ForgotPasswordToken") as string;
}

The commerceUser is not null and there is no error while setting the value.
Is there any "Save" action to be done just like it is done for Sitecore user profile - profile.Save()

Using XC 10.1

2
  • 1
    I think we can not set property in CommerceUser directly like this but you can set these property to Sitecore.Security.UserProfile, is there any specific reason you want to set these property to CommerceUser only? If yes, you can add a field to Customer Entity and retrieve from there. May 20 at 12:49
  • @GauravAgarwal After the for loop if I check commerceUser.GetProperties(), I'm able to see all the new properties.
    – Qwerty
    May 20 at 12:58
2

We added phoneNumber in CommerceUser, for that we used below code. You can update as per your requirement. But yes you need to save it into Customer Entity using below code.

public bool UpdateAccountPhone(string phone, IContext sitecoreContext)
{
    bool result = false;

    // Update phone number
    CustomerServiceProvider customerProvider =
    (CustomerServiceProvider)Factory.CreateObject(nameof(customerProvider), true);

    var commerceUser = this.GetCommerceUser(sitecoreContext);
    if (commerceUser != null)
    {
       commerceUser.SetPropertyValue("Phone", phone);
       // Update the user
       var updateRequest = new UpdateUserRequest(commerceUser);
       customerProvider.UpdateUser(updateRequest);
       result = true;
   }

   return result;
}

Use namespace below

using Sitecore.Commerce.Services;
using Sitecore.Commerce.Services.Customers;

Then you can get property like below -

commerceUser.GetPropertyValue("Phone").ToString();
1
  • How do we do it for a non-logged in user.
    – Qwerty
    May 20 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.