Partager via


How to Enumerate Attributes for a Profile Property

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

Profile property groups contain a collection of profile properties. Each profile property contains a collection of attributes. This topic shows how to enumerate the attributes of a profile property to retrieve the name and value of each attribute.

To enumerate attributes for a profile property

  1. Create an instance of the ProfileContext run-time object.

  2. Retrieve the profile based on a key value, such as UserID or eMail.

  3. Create a ProfilePropertyAttributeCollection collection of ProfileProperty attributes by specifying which ProfilePropertyGroup in the PropertyGroups collection to access.

  4. Iterate through each item in the ProfilePropertyAttributeCollection collection to retrieve its name and value.

Example

The following example shows how to enumerate a collection of attributes for a profile property. The example creates a procedure called GetAttributes that accepts the eMail address associated with a profile, the name of the property group, and the name of the property.

private void GetAttributes(string eMail, string propertyGroup, string property)
{
    // Get the Profiles run-time object.
    ProfileContext ctxt = CommerceContext.Current.ProfileSystem;

    // Get the profile from the eMail text box.
    Profile prof = ctxt.GetProfile("email_address", eMail, "UserObject");

    // Create a collection of profile property attributes.
    ProfilePropertyAttributeCollection attributes = prof.PropertyGroups[propertyGroup].Properties[property].Attributes;

    // Enumerate each attribute.
    foreach (ProfilePropertyAttribute attribute in attributes)
    {
        // Write out the attribute name and value.
        Console.WriteLine(attribute.Name);
        Console.WriteLine(attribute.Value);
    }
}

The following code shows how to call the GetAttributes procedure:

GetAttributes(eMail.Text, PropertyGroupBox.SelectedValue.ToString(), PropertyBox.SelectedValue.ToString());

To use this code to call the procedure, you must have a text box named eMail, a list box named PropertyGroupBox that contains a list of property groups, and a list box named PropertyBox that contains a list of properties.

Compiling the Code

  • To compile the code, you must include the following namespace directives:
using Microsoft.CommerceServer.Runtime;
using Microsoft.CommerceServer.Runtime.Profiles;

See Also

Other Resources

How to Enumerate Properties and Property Groups for a Profile Instance

Profiles Concepts and Tasks