How to Create an Instance of a Profile
Use the Profile object in your site code to create an instance of a profile.
The following list describes the prerequisites for creating an instance of a profile:
You must know which type of profile you are creating an instance of.
You must know the name of the profile property that is the primary key.
You must know the name of each required profile property.
To determine the type of profile you are creating an instance of
Click Start, point to Programs, point to Microsoft Commerce Server 2009 , and then click Commerce Server Manager.
In the left pane of Commerce Server Manager, expand Commerce Server Manager, expand Global Resources, expand Profiles, expand Profile Catalog, and then click Profile Definitions.
In the right pane, locate the row that contains the profile you want to create an instance of, and note the value in the Name column. For example, for the Address profile, the name is Address.
To determine the name of the profile property that is the primary key
Click Start, point to Programs, point to Microsoft Commerce Server 2009 , and then click Commerce Server Manager.
In the left pane of Commerce Server Manager, expand Commerce Server Manager, expand Global Resources, expand Profiles, expand Profile Catalog, expand Profile Definitions, and then click the name of the profile that you want to create an instance of.
In the right pane, expand each property group until you see a property that has a key icon before its name, and a D (for dual key) or P (for primary key) next to the key icon. This property represents the database key for the profile type. For example, in the Address profile, the key property is Address ID.
Note
For most Microsoft-defined profiles, the key property is in the General Information property group.
Click the property that has the key icon before its name that you selected in step 3.
In the Detailed Information section, note the value of the Name field. For example, in the Address profile, the name of the key field is address_id.
In the Properties section, click the property group that contains the key property.
In the Detailed Information section, note the value of the Name field. For example, in the Address profile, the key field is in the General Information property group, and the name of the General Information property group is GeneralInfo.
To create the name of the property that is the primary key, concatenate the name of the profile group, a dot (.), and the name of the primary key field. For example, the name of the property that is the primary key for the Address profile is GeneralInfo.address_id.
To determine the names of the profile properties that are required
Click Start, point to Programs, point to Microsoft Commerce Server 2009 , and then click Commerce Server Manager.
In the left pane of Commerce Server Manager, expand Commerce Server Manager, expand Global Resources, expand Profiles, expand Profile Catalog, expand Profile Definitions, and then click the name of the profile that you want to create an instance of.
In the right pane, expand a property group, and then click a property in the property group.
In the Detailed Information section, expand Advanced Attributes.
If the Required field is not selected, this property is not required. Select the next property, and repeat this procedure starting at step 3.
If the Required field is selected, this property is required. Note the value of the Name field at the top of the Detailed Information pane.
In the Properties section, click the property group that contains the required property.
In the Detailed Information section, note the value of the Name field for the property group.
To create the name of the property that is required, concatenate the name of the profile group, a dot (.), and the name of the required field.
Repeat steps 3 through 9 for every property in every property group, until you have determined the names of all the profile properties that are required.
Example
To create an instance of a profile, your code must:
Get a ProfileContext.
Create the instance of the profile, and provide a value for the profile property that is the key.
Assign values to the profile properties that are required.
Commit the changes to the underlying data storage.
This example creates an instance of the Address profile. The primary key for the Address profile is GeneralInfo.address_id. The following list identifies the required properties for an instance of an Address profile:
GeneralInfo.address_name
GeneralInfo.address_line1
GeneralInfo.region_code
GeneralInfo.postal_code
GeneralInfo.country_code
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.CommerceServer.Runtime;
using Microsoft.CommerceServer.Runtime.Profiles;
using CommerceProfile = Microsoft.CommerceServer.Runtime.Profiles.Profile;
namespace CSharpSite
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
// Get the profile context. We need this to work with
// instances of profiles.
ProfileContext profiles = CommerceContext.Current.ProfileSystem;
// Create an instance of the Address profile, and provide a
// unique key.
CommerceProfile newProfile = profiles.CreateProfile(Guid.NewGuid().ToString(), "Address");
// Assign values to the profile properties that are
// required.
newProfile["GeneralInfo.address_name"].Value = "Work";
newProfile["GeneralInfo.address_line1"].Value = "One Microsoft Way";
newProfile["GeneralInfo.region_code"].Value = "WA";
newProfile["GeneralInfo.postal_code"].Value = "98052";
newProfile["GeneralInfo.country_code"].Value = "USA";
// Commit the changes to the underlying data storage.
newProfile.Update();
}
}
}