CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeff Lynch [MVP]

Everything E-Commerce!

Commerce Server 2007: Development Tip #3 - Creating a New User

I'm going to repost this series of "development tips" related to Commerce Server 2007 to help other folks get started with their development work. All of these "tips" come directly from what I've learned (the hard way) over the past eight months and are intended to save you time and effort. All code samples are now based upon the RTM version and have been tested in our production environment.

Tip #3 - Creating a New User Account

Since the Commerce Server 2007 "Profiles" subsystem hasn't changed significantly from Commerce Server 2002, you can do this pretty simply by calling the CommerceContext.Current.ProfileSystem.CreateProfile method as shown in the sample code below.

public void CreateUser(string OrgId, 
                string Email, 
                string Password, 
                string FirstName, 
                string LastName, 
                string PasswordQuestion, 
                string PasswordAnswer,
                string UserRole)
    {
        // Create a new GUID
        string UserId = Guid.NewGuid().ToString();
 
        // Create a new UserObject Profile
        Profile profile = CommerceContext.Current.ProfileSystem.CreateProfile(UserId, "UserObject");
 
        // Set the required property values
        profile.Properties["GeneralInfo.email_address"].Value = Email;
        profile.Properties["GeneralInfo.user_security_password"].Value = Password;
        profile.Properties["GeneralInfo.user_type"].Value = 1;
        profile.Properties["GeneralInfo.last_name"].Value = LastName;
        profile.Properties["GeneralInfo.first_name"].Value = FirstName;
        profile.Properties["GeneralInfo.language"].Value = @"en_US";
        profile.Properties["GeneralInfo.password_question"].Value = PasswordQuestion;
        profile.Properties["GeneralInfo.password_answer"].Value = PasswordAnswer;
        profile.Properties["GeneralInfo.user_role"].Value = UserRole;
        profile.Properties["AccountInfo.org_id"].Value = OrgId;
        profile.Properties["AccountInfo.account_status"].Value = 1;
        profile.Properties["AccountInfo.date_registered"].Value = DateTime.Today;
 
        //Update and save the profile
        profile.Update();
    }

This basically involves creating  a new Profile object using a System.Guid, then setting the required properties' values and finally by calling the Profile.Update() method which persists the values back to the database. Unfortunately, the Profiles subsystem hasn't (yet) been updated to take advantage of strongly-typed properties like the Catalog subsystem has so these values are "weakly-typed" (indexer) properties.

In Commerce Server 2007, there are several other ways to accomplish this (you could use the Profiles Web Service for example or the new UpmMembershipProvider) but I've found this code to provide the simplest and most straight-forward method.

If you have other other ideas, please feel free to comment!

Technorati Tags:


Published Jul 12 2006, 03:10 PM by jlynch
Filed under:

Comments

jhuygen said:

The code runs fine in my project (no exceptions) but the changes are not commited to the database.  When I create a new profile or when I change an existing profile the changes are only available during the user's session.  Should I add something to my config or to my code?

Thanks,
Jelle
# June 14, 2006 9:46 AM

jhuygen said:

This is an update on my previous post: creating the user succeeds now, and updating default properties are also commited to the database.  There is only a problem with custom properties.  I added a property birthday to my profile.

I can access the property in code and I can update its value but changes are only available in the users session.  When I try to get the data when the session is terminated the property contains its old value.  Should custom properties be updated in an other way than default properties?

Thanks,
Jelle
# June 14, 2006 10:27 AM

jlynch said:

Jelle,

You'll need to modify the UserObject table and the User profile schema before you can use custom properties in your profiles. Take a look at the docs for instructions on doing this.

Jeff
# June 14, 2006 4:03 PM

Jeff Lynch [MVP] said:

What do I do if the StarterSite doesn't fit my scenario?

# January 4, 2007 8:28 PM

pragadeshwaran said:

Can any one help me out. What should be the value for profile.Properties["GeneralInfo.user_role"].Value = UserRole; UserRole?. Iam new to cs2007.

iam getting exception.What user role i have to mention.

# February 27, 2007 11:29 PM

jlynch said:

The GeneralInfo.user_role property is a custom property I added to the standard User profile to be used with the UpmRoleProvider (custom role provider) used on the site. The values I use are "Normal User" and "Admin".

Jeff

# February 28, 2007 8:28 AM

Vadul said:

Jeff, Do we really need to use the GUID? In the table it is a varchar(50). Do you see it breaking anywhere if we use an int cast as a string here? If we go this route, this would solve a lot of our data porting issues.
# January 31, 2008 11:32 AM

Padma said:

This is using the commerce context which is valid only in the case of commerce sites. How can we create or update profiles with out using the commerce context object model provided by Microsoft. I want to create and update profiles in a non commerce site (Type of solution). How do i achieve the same action by just adding the profilewebservices as web reference?
# March 17, 2008 4:30 AM

jlynch said:

Vadul,

Yes, the string must use a GUID since it is the primary key in the UserObject table. The code in my post was based upon a prerelease version of CS2007 and the correct string format is shown here:

// Create a new Profile GUID using the required format

StringBuilder sb = new StringBuilder();

sb.Append("{");

sb.Append(Guid.NewGuid().ToString());

sb.Append("}");

Jeff

# March 22, 2008 3:48 PM

jlynch said:

Padma,

You can use the Profile's web service to create and update user profiles outside of the web application. The CS2007 online docs explain how to do this using XML to create or update the user profile.

Jeff

# March 22, 2008 3:49 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

This Blog

Syndication

News