Udostępnij za pośrednictwem


How to Create an Instance of a Profile

For the latest version of Commerce Server 2007 Help, see the Microsoft Web site.

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

  1. Click Start, point to Programs, point to Microsoft Commerce Server 2007 , and then click Commerce Server Manager.

  2. 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.

  3. 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

  1. Click Start, point to Programs, point to Microsoft Commerce Server 2007 , and then click Commerce Server Manager.

  2. 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.

  3. 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.

  4. Click the property that has the key icon before its name that you selected in step 3.

  5. 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.

  6. In the Properties section, click the property group that contains the key property.

  7. 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.

  8. 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

  1. Click Start, point to Programs, point to Microsoft Commerce Server 2007 , and then click Commerce Server Manager.

  2. 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.

  3. In the right pane, expand a property group, and then click a property in the property group.

  4. In the Detailed Information section, expand Advanced Attributes.

  5. If the Required field is not selected, this property is not required. Select the next property, and repeat this procedure starting at step 3.

  6. 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.

  7. In the Properties section, click the property group that contains the required property.

  8. In the Detailed Information section, note the value of the Name field for the property group.

  9. 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.

  10. 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:

  1. Get a ProfileContext.

  2. Create the instance of the profile, and provide a value for the profile property that is the key.

  3. Assign values to the profile properties that are required.

  4. 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();
        }
    }
}

See Also

Other Resources

Profiles Concepts and Tasks

How to Access an Instance of a Profile