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 searchCriteria = new UserSearchCriteria();
    BASUserSearchCriteriaServiceAttributes basServiceAttributes = new BASUserSearchCriteriaServiceAttributes();
    basServiceAttributes.displayName = userDisplayName;
    searchCriteria.userSearchCriteriaServiceAttributes = new UserSearchCriteriaServiceAttributesType[1];
    searchCriteria.userSearchCriteriaServiceAttributes[0] = basServiceAttributes;
  2. Specify the order that you want the results returned in.
    BASUserSearchResultSortByEnum sortBy = new BASUserSearchResultSortByEnum();
    sortBy.@enum = BASUserSearchResultSortByEnumType.DISPLAY_NAME;
    sortBy.enumSpecified = true;
    findUsersRequest.sortBy = sortBy;
    findUsersRequest.sortAscendingOrder = 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.
    FindUsers findUsersRequest = new FindUsers();
    findUsersRequest.locale.pageSize = 3;
    findUsersRequest.lastResultElement = null; 
    findUsersRequest.locale = locale;
    findUsersRequest.searchCriteria = searchCriteria;
  4. Invoke findUsers().
    findUsersResponse response = coreWebService.findUsers(findUsersRequest);
    findUsersResult result = response.returnValue;
  5. Check the result for errors and handle any errors.
    if (result.findUsersReturnStatus.code != 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.userSearchResult != null && result.userSearchResult!=null)
    {
        for (int i = 0; i < result.userSearchResult.Length; i++)
        {
           UserSearchResult searchResult = result.userSearchResult[i];
           UserSearchResultServiceAttributesType[] serviceAttributes = searchResult.userSearchResultServiceAttributes;
           for (int j = 0; j < serviceAttributes.Length; j++)
           {
                if (serviceAttributes[j].GetType().Equals(typeof(BASUserSearchResultServiceAttributes)))
                {
                    BASUserSearchResultServiceAttributesType BASAttributes = serviceAttributes[j] as BASUserSearchResultServiceAttributesType;
     
                    String currentUserID = BASAttributes.userId;
                    // Do something with the user ID here. 
                }      
           }  
        }
        lastResultElement = response.returnValue.userSearchResult[response.returnValue.userSearchResult.Length - 1];
        findUsersRequest.lastResultElement = lastResultElement;
    
        response = coreWebService.findUsers(findUsersRequest);
        result = response.returnValue;
    }

Was this information helpful? Send us your comments.