Bagikan melalui


How to Enumerate Properties and Property Groups for a Profile Instance

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 explains how to enumerate through profile property groups and through each profile property to retrieve its name and value.

To enumerate properties and property groups

  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 function that implements the IEnumerable interface. This function uses C# iterators to enumerate all ProfileProperty objects in the ProfilePropertyCollection collection. It also enumerates all ProfilePropertyGroup objects in the ProfilePropertyGroupCollection collection.

Example

The following example shows how to enumerate the profile properties and property groups for a profile. The example creates a procedure called GetProperties that accepts the e-Mail address associated with a profile and also a procedure called EnumerateProperties which is used to enumerate the profile properties and property groups.

private void GetProperties(string eMail)
{
    ProfileContext ctxt = CommerceContext.Current.ProfileSystem;
    Profile profile = ctxt.GetProfile("GeneralInfo.email_address", eMail, "UserObject");
    foreach (ProfileProperty property in EnumerateProperties(profile))
    {
        Console.WriteLine(
            "{0} = {1}",
            property.Name,
            property.Value);
    }
}

private IEnumerable<ProfileProperty> EnumerateProperties(IProfileElements elements)
{
    if (elements == null)
    {
        throw new ArgumentNullException("elements");
    }

    foreach (ProfileProperty property in elements.Properties)
    {
        yield return property;
    }

    foreach (ProfilePropertyGroup group in elements.PropertyGroups)
    {
        foreach (ProfileProperty property in EnumerateProperties(group))
        {
            yield return property;
        }
    }
}

The following code shows how to call the GetProperties procedure:

GetProperties(eMail.Text);

To use this code to call the procedure, you must have a text box named eMail.

Compiling the Code

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

See Also

Other Resources

How to Enumerate Attributes for a Profile Property

Profiles Concepts and Tasks