How to Access an Instance of a Profile

Use the ProfileContext object in your site code to retrieve an instance of a profile. After you have retrieved the profile instance, you can access or modify its properties by using the Profile object. Be sure to call the Update method to commit the changes to the underlying data storage if you modify the profile instance.

Use the DeleteProfile method to delete an instance of a profile.

The following code example first creates an instance of a profile, retrieves the profile instance, retrieves a property of the profile instance, modifies a property of the profile instance, and finally deletes the profile instance.

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 access to the collection of profile instances.

            ProfileContext profiles = CommerceContext.Current.ProfileSystem;

            // To make the example work, create a new instance of a
            // profile, so that we know its profileID.

            string profileID = Guid.NewGuid().ToString();
            CommerceProfile newProfile = profiles.CreateProfile(profileID, "Address");
            newProfile["GeneralInfo.address_name"].Value = "Home";
            newProfile["GeneralInfo.address_line1"].Value = "street";
            newProfile["GeneralInfo.region_code"].Value = "region";
            newProfile["GeneralInfo.postal_code"].Value = "code";
            newProfile["GeneralInfo.country_code"].Value = "country";
            newProfile.Update();

            // Retrieve the profile instance.

            Profile myProfile = profiles.GetProfile(profileID, "Address");

            // Get the value of a property of the profile instance.

            string postalCode = myProfile["GeneralInfo.postal_code"].Value.ToString();

            // Set the value of a property of the profile instance.

            myProfile["GeneralInfo.postal_code"].Value = "12345";

            // Commit the changed postal code to the underlying data
            // storage. 

            myProfile.Update();

            // Delete the instance of the profile.

            profiles.DeleteProfile(profileID, "Address");
        }
    }
}

See Also

Other Resources

Profiles Concepts and Tasks

How to Create an Instance of a Profile