Search for user account IDs

To search for user account IDs, you can use any search criteria in the classes that extend the UserSearchCriteriaServiceAttributesType class, such as device PIN using the BASUserSearchCriteriaServiceAttributesType class and email address in the EmailCommonUserSearchCriteriaServiceAttributesType class. The findUsers() method might return multiple users account IDs if the search criteria is not unique. The findUsers() method automatically adds wildcard characters to the beginning and end of each search term. For example, if you search for user accounts' BlackBerry® device PIN using the string "1", the BlackBerry® Administration API returns a result that contains all users with a BlackBerry device PIN that contains a one. The following code sample uses the user accounts' display names as the search criteria.
Before you begin:

Retrieve the userDisplayName string for the display name of the user you are trying to find.

  1. Specify the search criteria using the displayName attribute in the BASUserSearchCriteriaServiceAttributesType class. To specify the search criteria, you can use any of the classes that extend the UserSearchCriteriaServiceAttributesType class. Each class that extends the UserSearchCriteriaServiceAttributesType class contains additional search criteria that you can specify for your search. The number of search criteria that are available to you depends on the components that are installed on your BlackBerry® Enterprise Server. For more information about the available search criteria, see the UserSearchCriteriaServiceAttributesType class in the API reference.
    UserSearchCriteria userSearchCriteria=new UserSearchCriteria(); 
    BASUserSearchCriteriaServiceAttributes BASCriteria=new BASUserSearchCriteriaServiceAttributes();
  2. Specify the order that you want the results returned in.
    BASUserSearchResultSortByEnum sortByEnum=new BASUserSearchResultSortByEnum();
    boolean sortAscending=true;
  3. Specify the maximum number of results returned for each page, the locale, and the last result element. For your first search request, assign a null value for the last result element.
    int pageSize = 5;
    String locale = locale;
    UserSearchResult lastResultElement = null;
  4. Invoke findUsers().
    FindUsersResult result = coreWebService.findUsers(userSearchCriteria, sortByEnum, sortAscending, locale, lastResultElement, pageSize);
  5. Check the result for errors and handle any errors.
    if (result.getFindUsersReturnStatus().getCode()!=FindUsersReturnStatusEnumType.SUCCESS) 
        //handle any errors
  6. Loop through the search results to get the ID of each user who matches the search criteria. To retrieve the page of results, specify the last result element from the previous findUsers() request and invoke findUsers() again.
    while (result.getFindUsersReturnStatus().getCode() == FindUsersReturnStatusEnumType.SUCCESS && result.getUserSearchResult()!=null) 
        List<UserSearchResult> results=result.getUserSearchResult();
        for (UserSearchResult itr:results) 
            for (UserSearchResultServiceAttributesType itr2: itr.getUserSearchResultServiceAttributes()) 
                if (itr2 instanceof BASUserSearchResultServiceAttributesType) 
                    BASUserSearchResultServiceAttributesType BASAttributes=(BASUserSearchResultServiceAttributesType) itr2;
                    String currentUserID = BASAttributes.getUserId(); 
                    // Do something with the user ID here. 
         //fetch the next page by passing in the last UserSearchResult object in the previous list
        result=coreWebService.findUsers(userSearchCriteria, sortByEnum, sortAscending, locale, result.getUserSearchResult().get(result.getUserSearchResult().size()-1), pageSize); 

Was this information helpful? Send us your comments.