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: Commerce Server