Commerce server profile will provide lot of advantages to developer by allowing extending it as per client business needs. In this article we are discussing how to add an image to use profile. Personalization is currently hot topic in the market and in this article we are extending our profile system to store user image in his profile. This image can be used to display in reviews/communities in ecommerce site.

There are two days we can implement this solution

1.       Store image in file system and the file path in database.

2.       Store image in the database.     

In the current article we are storing image in the database.

Follow these steps

1.       Create a new field named “u_UserImage” of type “Image” in UserObject table (This is  used to store user image).

ALTER TABLE dbo.UserObject ADD

                u_UserImage image NULL

GO

2.       Open commerce server manager. Expand Profile-> profile Catalog-> Data Sources-> ProfileService_SQLSource->DataObjects-> UserProfile.

3.       Right click on User Object and click on “Add Data Member” and enter text as shown in the image.

4.       Open commerce server manager. Expand Profile-> profile Catalog-> Profile Definitions and click on User object.
Add Data Member

5.       Click on General Information and click on Add Property and add values as shown in the image below.

 

Add Property
6.
      
In your registration page, add a input field of type file.

7.      

Add following code at submit button click event.

 

// Reading file content.

Stream s = File.OpenRead(filePath);
byte[] buffer = new byte[s.Length];
s.Read(buffer, 0, (int)s.Length);
s.Dispose();
s.Close();      

//Creating an user profile.

Profile userProfile = profileContext.CreateProfile(newId.ToString(), “UserObject”);
userProfile.Properties["GeneralInfo.user_id"].Value = newId.ToString();
userProfile.Properties["GeneralInfo.first_name"].Value = txtbox_FirstName.Text.ToString();
userProfile.Properties["GeneralInfo.last_name"].Value = txtbox_LastName.Text.ToString();
userProfile.Properties["GeneralInfo.email_address"].Value = txtbox_Email.Text.ToString();
userProfile.Properties["GeneralInfo.user_security_password"].Value = txtbox_Password.Text.ToString();
userProfile.Properties["GeneralInfo.user_type"].Value = 1;
userProfile.Properties["GeneralInfo.language"].Value = “en-US”;
userProfile.Properties["GeneralInfo.password_question"].Value = txtbox_PasswordAns.Text.ToString();
userProfile.Properties["GeneralInfo.password_answer"].Value = txtbox_PasswordAns.Text.ToString();
userProfile.Properties["GeneralInfo.image"].Value = buffer;   
userProfile.Update();

 

If you would like to have multiple images for a profile then better to create a table for all profile images. Each entry will have a unique ID and image content. Create a data object in commerce server. Extend userObject to hold a array of user images – i.e., unique Ids(as similar to storing multiple address).

Related Articles


If you like this post, please click on our sponsor advertisement.


3 Responses to “Adding Image to Profile Sub-System”

  • Kris:

    Hi,
    I tried the above steps but couldn’t work out. I am getting an error “The value System.Byte[] of the property GeneralInfo.UserImage is not of the correct data type Binary”. Please help on this.

    regards,
    Kris

  • The answer to your problem will og if you change “personal_image” property data type from “binary” to “long binary” – and reset your IIS.

    -Ravi Kanth

  • Can you download the code from the below link and check where you went wrong.

    http://cid-
    f348d7145d1be6c2.skydrive.live.com/self.aspx/Code/Extending%20User%20Profile.zip

    Many people tried this code and it worked well for them.

    -Ravi Kanth

Leave a Reply

Follow me on Google+
Couldn't get data from google+
Sign up for Newsletter