Partager via


How to Delete Multiple Profiles

You may have to delete multiple profiles, based on specific criteria. For example, you may have to delete the profiles for all users who belong to a specific company. The process of deleting multiple profiles consists of two parts:

  1. Creating a DataSet object that contains the profiles you want to delete. The DataSet is created by searching for profiles that match specific criteria.

  2. Looping through the DataSet rows and deleting the profiles individually.

This topic shows how to delete all profiles in the Profiles System, but the criteria can easily be modified to match any criteria that you want.

To delete multiple profiles

  1. Create an instance of the ProfileManagementContext class.

  2. Call the GetSearchableEntities method of the ProfileManagementContext class and assign the result to a DataSet object.

    This creates a list of entities that can be searched.

  3. Create a SearchClauseFactory object for the UserObject profile.

  4. Create a SearchClause object and specify the conditions for which you will search the Profiles System.

    This topic shows how to search for all e-mail addresses that are not null, but you can modify the conditions to suit your needs.

  5. Create a new SearchOptions object and set any applicable properties for your search.

    The example in this topic sets the PropertiesToReturn property to "user_id" because that property will uniquely identify a profile and is passed into the ExecuteSearch method.

  6. Execute a search based on the criteria you specified by calling the ExecuteSearch method and pass the search criteria and options.

    The results of this search are put into a DataSet.

  7. Using the foreach function in C#, loop through all rows in the DataSet returned from executing the search. For each row, retrieve the user_id value.

  8. Pass the user_id value into the DeleteProfile method to delete a user profile.

Example

The following code example shows how to use the Profiles Web service to create a ProfileManagementContext object. The object is used to search for all profiles in the system by specifying that the e-mail address is not null. The results of this search are stored in a DataSet object. The code loops through the rows in the DataSet, deleting each profile one-by-one.

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

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

// Build search clause for the UserObject.
SearchClauseFactory factory =
    pmc.GetSearchClauseFactory(searchableEntities, "UserObject");
SearchClause searchClause = 
    factory.CreateClause(ExplicitComparisonOperator.NotEqual, 
    "email_address", System.DBNull.Value);

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

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

// Loop through each row in the dataset.
foreach (DataRow dataRowInstance in searchResults.Tables[0].Rows)
{
    // Retrieve the user_id from the dataset.
    string dr = dataRowInstance["GeneralInfo.user_id"].ToString();

    // Delete the profile
    pmc.DeleteProfile(dr, "UserObject");
}

Compiling the Code

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

  • using Microsoft.CommerceServer;

  • using Microsoft.CommerceServer.Profiles;

  • using System.Data;

See Also

Other Resources

Profiles Concepts and Tasks

Developing with the Profiles System