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 searchCriteria = new UserSearchCriteria();
    BASUserSearchCriteriaServiceAttributes basServiceAttributes = new BASUserSearchCriteriaServiceAttributes();
    basServiceAttributes.application = applicationName;
    searchCriteria.userSearchCriteriaServiceAttributes = new UserSearchCriteriaServiceAttributesType[1];
    searchCriteria.userSearchCriteriaServiceAttributes[0] = basServiceAttributes;
    findUsersRequest.searchCriteria = searchCriteria;
    
  2. Specify the order that you want the BlackBerry® Administration API to return the results 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 that the BlackBerry Administration API returns for each page, specify the locale that you are using, and specify lastResultElement as null.
    UserSearchResult lastResultElement=null;
    findUsersRequest.locale.pageSize = 3;
    findUsersRequest.lastResultElement = lastResultElement; 
    findUsersRequest.locale = "en_US";
  4. Invoke findUsers(). Check the result for errors and handle any errors
    findUsersResponse response = coreWebService.findUsers(findUsersRequest);
    findUsersResult result = response.returnValue;
    if (result.findUsersReturnStatus.code != 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.findUsersReturnStatus.code == FindUsersReturnStatusEnumType.SUCCESS) && (result.userSearchResult != null))
    {
        for (int i = 0; i < response.returnValue.userSearchResult.Length; i++)
        {
           UserSearchResult searchResult = response.returnValue.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;
     
                    int currentUserID = BASAttributes.userId;
                    // 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.userSearchResult[response.returnValue.userSearchResult.Length - 1];
        findUsersRequest.lastResultElement = lastResultElement;
        response = coreWebService.findUsers(findUsersRequest);
        result = response.returnValue;
    }
  7. Check the result for errors and handle any errors.
    if(result.findUsersReturnStatus.code != FindUsersReturnStatusEnumType.SUCCESS)
    {
        // handle any errors
    }

Was this information helpful? Send us your comments.