Search for user accounts that have a specific application on their BlackBerry devices

You can use the application attribute to search using an application name, or the applicationId attribute to search using an application ID integer. Both attributes are part of the BASUserSearchCriteriaServiceAttributes class. The following code sample searches using the application name.
Before you begin: Retrieve the applicationName string for the name of the application you want to find.
  1. Create the search criteria.
    UserSearchCriteria userSearchCriteria = new UserSearchCriteria(); 
    BASUserSearchCriteriaServiceAttributes BASCriteria = new BASUserSearchCriteriaServiceAttributes();
    BASCriteria.setApplication(applicationName);
    userSearchCriteria.getUserSearchCriteriaServiceAttributes().add(BASCriteria);
  2. Specify the order that you want the BlackBerry® Administration API to return the results in.
    BASUserSearchResultSortByEnum sortByEnum = new BASUserSearchResultSortByEnum();
    sortByEnum.setEnum(BASUserSearchResultSortByEnumType.DISPLAY_NAME); 
    boolean sortAscending=true;
  3. Specify the maximum number of results that the BlackBerry Administration API returns for each page, specify the locale that you are using, and specify lastResultElement as null.
    int pageSize=5;
    String locale="en_US";
    UserSearchResult lastResultElement=null;
  4. Invoke findUsers(). Check the result for errors and handle any errors
    FindUsersResult result=coreWebService.findUsers(userSearchCriteria, sortByEnum, sortAscending, locale, lastResultElement, pageSize);
    if (result.getFindUsersReturnStatus().getCode() != FindUsersReturnStatusEnumType.SUCCESS) 
    {  
        //handle any errors
    }
  5. Loop through the result to get the ID of each user account that matches the search criteria.
    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;
                    
                    int currentUserID = BASAttributes.getUserId(); 
                    // Do something with currentUserID here. For example, store it in an array.  
                }
            }
         }
  6. Specify the last result element from the previous findUsers() request and invoke findUsers() again to repeat the loop.
        lastResultElement = result.getUserSearchResult().get(result.getUserSearchResult().size()-1);
        result=coreWebService.findUsers(userSearchCriteria, sortByEnum, sortAscending, locale, lastResultElement, pageSize); 
    }
  7. Check the result for errors and handle any errors.
    if(result.getFindUsersReturnStatus().getCode() != FindUsersReturnStatusEnumType.SUCCESS)
    {
        // handle any errors
    }

Was this information helpful? Send us your comments.