Partager via


How to Search Profile Data

Use the ProfileManagementContext object in your site code to retrieve an instance of a profile from the Profiles Web Service. After you have retrieved the ProfileManagementContext, you can execute a search for any Profiles System property. Before you can execute a search, you must create a search clause using the SearchClauseFactory object and set its properties.

To search profile data

  1. Create an instance of the ProfileManagementContext class by calling the Create method and passing it the URL of the Profiles Web Service.

  2. Create an instance of a SearchOptions object to specify options that will be made for searching profile data.

  3. Get a list of searchable entities for a profile by calling the GetSearchableEntities method of the ProfileManagementContext object.

  4. Build a search clause factory by calling the GetSearchClauseFactory method of the ProfileManagementContext object.

  5. Create a search clause by calling the CreateClause method of the SearchClauseFactory object and specifying the values to search.

  6. Specify the search options using the SearchOptions object that you created earlier.

  7. Begin the search by calling the ExecuteSearch method of the ProfileManagementContext object.

Example

The following code example shows how to create a function (using Agent Mode) that returns the Profiles System user_id field when it is passed a string containing the e-mail address of a registered user. The code creates a ProfileManagementContext object from the Profiles Web Service. It uses this object to execute a search against the Profiles database.

private string GetProfileUserID(string emailAddress)
{
    // Declare return variable.
    string returnValue = String.Empty;

    // Declare ProfileManagementContext.
    ProfileManagementContext profile;

    // Instantiate ProfileManagementContext as an instance of the 
    // Profiles Web service.
    profile = 
      ProfileManagementContext.Create("https://localhost/ProfilesWebService/ProfilesWebService.asmx");

    // Declare and instantiate SearchClause and SearchOptions objects.
    SearchClause searchClause = null;
    SearchOptions searchOptions = new SearchOptions();

    // Build dataset of searchable entities for a profile.
    DataSet searchableEntities = profile.GetSearchableEntities();

    // Build search clause for the UserObject.
    SearchClauseFactory factory = 
      profile.GetSearchClauseFactory(searchableEntities, "UserObject");
    searchClause = 
      factory.CreateClause(ExplicitComparisonOperator.Equal, 
      "email_address", emailAddress);

    // Set number of records to return.
    searchOptions.NumberOfRecordsToReturn = 1;

    // Specify profile properties to return in the search dataset.
    searchOptions.PropertiesToReturn = "user_id";

    // Execute the search against the Profiles Web service.
    DataSet searchResults = profile.ExecuteSearch("UserObject", 
        searchClause, searchOptions);

    // Return results from function to calling procedure.
    if (searchResults.Tables[0].Rows.Count > 0)
    {
        returnValue = 
          Convert.ToString(
              searchResults.Tables[0].Rows[0]["GeneralInfo.user_id"]);
    }

    return returnValue;
}

When you specify the user_id field, you need to fully qualify the name as GeneralInfo.user_id.

If you search a TIME field for a specific time value, you must also include a default date value of 1899-12-30. This is because the underlying database stores a TIME value in a DATETIME field, which requires a date.

Compiling the Code

To compile the code, you need to include these namespace directives:

  • using Microsoft.CommerceServer;

  • using Microsoft.CommerceServer.Profiles;

See Also

Other Resources

Profiles Concepts and Tasks

Understanding the Different Types of Commerce Server APIs