Try our conversational search powered by Generative AI!

How to add new properties to a CustomerProfile

Vote:
 

Hello there,

In my web.config I have defined a profile that inherits from Mediachase.Commerce.Customers.Profile.CustomerProfile (for working with the CommerceManager). I can store information like Email, FirstName... but I can't store custom data like a Code in this way:

this.CurrentProfile =
                    EPiServerProfile.Get(
                        CustomerContext.Current.GetUserForContact(CustomerContext.Current.CurrentContact).UserName);

this.CurrentProfile["Code"] = "Code";
this.CurrentProfile.Save()

    

The "Code" data is not stored although I have this property defined in my web.config, and definitely data is not stored in dbo.aspnet_* tables. How can I add new properties to the CustomerProfile? My objective is to use these custom properties in both CMS and Commerce Manager.

Here is my web.config profile part:

<profile enabled="true" defaultProvider="CMSProfileProvider" inherits="Mediachase.Commerce.Customers.Profile.CustomerProfile, Mediachase.Commerce" >
      <providers>
        <clear />
        <add name="CMSProfileProvider" type="Mediachase.Commerce.Customers.Profile.Providers.CustomerSqlProfileProvider, Mediachase.Commerce" connectionStringName="EcfSqlConnection" applicationName="farmacia.cm.local" />
		<add name="SqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="EcfSqlConnection" applicationName="farmacia.cm.local" />
      </providers>
      <properties>
		<add name="MarketId" type="System.String" allowAnonymous="true" />
		<add name="WholesalersData" type="System.Collections.Generic.List`1[System.String]" />
		<add name="Code" type="System.String" />
		<add name="NumberColegiado" type="System.String" />
                <!-- <add allowAnonymous="false" defaultValue="" name="PageSettings" readOnly="false" serializeAs="ProviderSpecific" type="Mediachase.Commerce.Customers.Profile.CMPageSettings" />
                <add allowAnonymous="true" defaultValue="" name="ConsoleUILanguage" readOnly="false" serializeAs="ProviderSpecific" type="System.String" /> -->
      </properties>
    </profile>

    

#81829
Feb 27, 2014 10:57
Vote:
 

So, you can't get your property or you need store them in dbo.aspnet_* ?

If just get user profile, I config like this and it works:

<profile enabled="true" defaultProvider="SqlProfileProvider" automaticSaveEnabled="false">
      <properties>
        <add name="Code" type="System.String" />
        <add name="MarketId" type="System.String" allowAnonymous="true" />
      </properties>

After CurrentProfile was saved, you can get it using GetPropertyValue:

var profile = EPiServerProfile.Get("UserName");
profile["Code"] // This returns Code property.

    

#82012
Edited, Mar 04, 2014 9:46
Vote:
 

I have no experience of using EPiServerProfile.Get with Commerce, so I can't comment on problems that may or may not occur.

When working with commerce I have always used the Commerce CustomerProfileWrapper class and I have not experience any issues surrounding its use with further custom properties.

 

var customerProfile = Security.Context.Current.CurrentUserProfile as CustomerProfileWrapper;

 

I hope this helps a bit.

#82222
Mar 07, 2014 15:37
Vote:
 

@Son Do I already tried modifying the web.config from Commerce Manager in the same shape as your suggestion. It worked, yet as it doesn't inherit from CustomerProfile class anymore, it broke the Commerce Manager by raising exceptions everywhere telling me that several properties were missing.

@Martin Pickering I'll give it a try to your solution when I have a moment to spare (although I can't use the "Current" property as our objective is to implement new reports for CM and for every customer in the system). For the time being, I have decided to solve my issues by using a Business Foundation approach and modify the default Contact object so it includes the Code property I required (alongside being able to edit it from the CM at the Contact menu).

Thanks for the answers!

#82254
Edited, Mar 10, 2014 9:40
Vote:
 

OK :) I've re-checked your config, It worked as our expected.

My added config:

<profile enabled="true" defaultProvider="CMSProfileProvider" inherits="Mediachase.Commerce.Customers.Profile.CustomerProfile, Mediachase.Commerce">
          <providers>
              <clear />
              <add name="CMSProfileProvider" type="Mediachase.Commerce.Customers.Profile.Providers.CustomerSqlProfileProvider, Mediachase.Commerce" connectionStringName="EcfSqlConnection" />
          </providers>
          <properties>
              <!-- ECF End -->
              <add name="MarketId" type="System.String" allowAnonymous="true" />

              <add name="WholesalersData" type="System.Collections.Generic.List`1[System.String]" />
              <add name="Code" type="System.String" />
              <add name="NumberColegiado" type="System.String" />
              <!-- <add allowAnonymous="false" defaultValue="" name="PageSettings" readOnly="false" serializeAs="ProviderSpecific" type="Mediachase.Commerce.Customers.Profile.CMPageSettings" />
                <add allowAnonymous="true" defaultValue="" name="ConsoleUILanguage" readOnly="false" serializeAs="ProviderSpecific" type="System.String" /> -->
          </properties>
      </profile>

    

And to get properties we can do like this:

var profile = EPiServerProfile.Get(SecurityContext.Current.CurrentUserName);
            profile["Code"] = "code-" + SecurityContext.Current.CurrentUserName; // This is sample to adding Code property
            profile.Save();

            myCode.Text = profile["Code"].ToString(); // This returns Code property.

    

I hope this can help :)

#82286
Mar 10, 2014 10:39
Vote:
 

@Son Do thanks for your answer. Unafortunately, your approach, very strangely, doesn't work as I expect, it doesn't save any value I provide. I propose you to do the following:

Execute the following code of yours:

var profile = EPiServerProfile.Get(SecurityContext.Current.CurrentUserName);
profile["Code"] = "code-" + SecurityContext.Current.CurrentUserName; // This is sample to adding Code property
profile.Save();
myCode.Text = profile["Code"].ToString(); // This returns Code property.


    

The execute the following code. The "Code" property should still be there for a given profile:

var profile = EPiServerProfile.Get(SecurityContext.Current.CurrentUserName);
myCode.Text = profile["Code"].ToString(); // This returns Code property.

    

Re-Login with the given username, reexecute previous code, and the Code property magically disappears. This is very rare and unexpected.

I would also wish to know where the profile data gets saved, because they are not in the aspnet tables

 

Thanks!

 
#82293
Mar 10, 2014 11:11
Vote:
 

Uhm, you're right about it. We cannot get profile by username right now, I will explain this case below. First is my solution:

You can save it via EPiServerProfile.Current

EPiServerProfile profile = EPiServerProfile.Current;
profile["Code"] = "code-" + profile.UserName;

And it can be get very simple:

EPiServerProfile profile = EPiServerProfile.Current;
profile["Code"] // Property Code.

    

Your question, where is profile data saved? As I known those properties was save in aspnet_Profile as usual. They're custom properties, so they're saved as binary.

And  EPiServerProfile.Get(SecurityContext.Current.CurrentUserName) can't be used on this case because Commerce create 2 user for each 'real' user - they'll be combined to use in Front-end and Back-end site.

I hope it works now :)

#82297
Edited, Mar 10, 2014 12:35
Vote:
 

@Son Do thanks for your reply. Yet, I'm curious about one thing. If I would need to iterate over several Customer Profiles (which is my case) then how can I retrieve them without using the "Get" function on a loop? I guess that the "Current" property is not user in that case, am I right?

By the way, What's the reason about profile data being removed when logging in?

Thank you and kind regards,

David

 

#82310
Edited, Mar 10, 2014 16:03
Vote:
 

I think you're right again, we cannot use "Current" in your case and sadly I'm not sure we can get list all or several customer profiles.

About profile data is removed, in my opinion, it is not removed :) I just think when we use EPiServerProfile.Get(SecurityContext.Current.CurrentUserName), it just can't get exactly profile we want or it can't save data properly using EPiServerProfile.Save() after Get().

#82324
Mar 11, 2014 5:36
Vote:
 

@Son Do given these constraints about using the profiles, I have decided to crash my head against the Business Foundation objects approach. Let's see how this path works :). Thank you!

#82785
Mar 19, 2014 16:03
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.