Share via


Receive a Search Response

The following code example demonstrates how to receive a user search response. The operations in the Initialize RTC and Register to Receive Events (RTCE_USERSEARCH events) code examples must be performed before using this example.

C++ Code Example

IRTCUserSearchResultsEvent *pUserSearchResultsEvent = NULL;
IRTCEnumUserSearchResults  *pUserSearchResults = NULL;
IRTCUserSearchResult       *pUserSearchResult = NULL;

// This returns the status of the search.
LONG   lStatusCode;
// This is used to match the return object with the request made.
LONG  lReturnCookie;
ULONG ulFetched = 0;
// This Boolean value informs the user of more possible results.
VARIANT_BOOL  fMoreAvailable = FALSE;
// These strings are for storing the search results.
BSTR  bstrURI = NULL;
BSTR  bstrDisplayName = NULL;
BSTR  bstrTitle = NULL;
BSTR  bstrOffice = NULL;
BSTR  bstrPhone = NULL;
BSTR  bstrCompany = NULL;
BSTR  bstrCity = NULL;
BSTR  bstrState = NULL;
BSTR  bstrCountry = NULL;
BSTR  bstrEmail = NULL;
// This HRESULT is for receiving errors.
HRESULT   hr = 0;

// Create the UserSearch results event object from 
// the IDispatch object returned with the event.
hr = pIDispatch->QueryInterface( IID_IRTCUserSearchResultsEvent, 
                                 reinterpret_cast<void**> 
                                 (&pUserSearchResultsEvent));

// Release the IDispatch object.

// If (hr != S_OK), process the error here.

// Retrieve the cookie from the search.

hr = pUserSearchResultsEvent->get_Cookie(&lReturnCode);

// If (hr != S_OK), process the error here.

// You can use this cookie to match the response with 
// the request.

// Retrieve the status code from the search.
hr = pUserSearchResultsEvent->get_StatusCode(&lStatusCode);

// If (hr != S_OK), process the error here.

// If (lStatusCode != S_OK), the UserSearch operation failed;
// process the error here.

// Check whether there are more results available.
hr = pUserSearchResultsEvent->get_MoreAvailable(&fMoreAvailable);

// If (hr != S_OK), process the error here.

// If (fMoreAvailable), there might be more results 
// available if the search is narrowed.

// Enumerate through the user search results.
hr = pUserSearchResultsEvent->EnumerateResults(&pUserSearchResults);

// Release the IRTCUserSearchResultsEvent object.

// If (hr != S_OK), process the error here.

while( S_OK == pUserSearchResults->Next(1, &pUserSearchResult, &ulFetched) )
{
    hr = pUserSearchResult->get_Value(RTCUSC_URI, &bstrURI);

    // If (hr != S_OK), process the error here.

    hr = pUserSearchResult->get_Value(RTCUSC_DISPLAYNAME, &bstrDisplayName);

    // If (hr != S_OK), process the error here.

    hr = pUserSearchResult->get_Value(RTCUSC_TITLE, &bstrTitle);

    // If (hr != S_OK), process the error here.

    hr = pUserSearchResult->get_Value(RTCUSC_OFFICE, &bstrOffice);

    // If (hr != S_OK), process the error here.

    hr = pUserSearchResult->get_Value(RTCUSC_PHONE, &bstrPhone);

    // If (hr != S_OK), process the error here.

    hr = pUserSearchResult->get_Value(RTCUSC_COMPANY, &bstrCompany);

    // If (hr != S_OK), process the error here.

    hr = pUserSearchResult->get_Value(RTCUSC_CITY, &bstrCity);
	
    // If (hr != S_OK), process the error here.

    hr = pUserSearchResult->get_Value(RTCUSC_STATE, &bstrState);

    // If (hr != S_OK), process the error here.

    hr = pUserSearchResult->get_Value(RTCUSC_COUNTRY, &bstrCounty);

    // If (hr != S_OK), process the error here.

    hr = pUserSearchResult->get_Value(RTCUSC_EMAIL, &bstrEmail);

    // If (hr != S_OK), process the error here.

    // Add the necessary code for printing or processing 
    // strings here.

    // Release IRTCUserSearchResult and all BSTRs 
    // before the end of the loop.

}

// Release the IRTCEnumUserSearchResults object.

FakePre-3c7d75f1a6b049768e8d831719181815-c1ef5b6efd804f21903f4a5d931984a6

Visual Basic Code Example

' The application must perform the operations in the
' "Initialize RTC"  and "Register to Receive Events"
' (RTCE_USERSEARCH events) before using this example.

' Set the error handling routine here. 
' On Error GoTo MyErrorRoutine 

Dim objUserSearchResultsEvent  As IRTCUserSearchResultsEvent
Dim objUserSearchCollection    As IRTCCollection
Dim objUserSearchResult        As IRTCUserSearchResult

Dim lStatusCode      As Long
Dim lCookie          As Long
Dim lResultCount     As Long
Dim lResultLoopVar   As Long
Dim fMoreAvailable   As Boolean
Dim strURI           As String
Dim strDisplayName   As String
Dim strTitle         As String
Dim strOffice        As String
Dim strPhone         As String
Dim strCompany       As String
Dim strCity          As String
Dim strState         As String
Dim strCountry       As String
Dim strEmail         As String

' objEvent is the IDispatch object that was returned 
' with the event. The IRTCUserSearchResultsEvent can  
' be extracted from it if the event was an 
' RTCE_USERSEARCH event.  
Set objUserSearchResultsEvent = objEvent

' Retrieve the cookie from the search. You can use 
' this cookie to match the response with the request.
lCookie = objUserSearchResultsEvent.Cookie

' Retrieve the status code from the search.
lStatusCode = objUserSearchResultsEvent.StatusCode

' If (Hex(lStatusCode) > 0) the UserSearch operation
' failed, process the error here. 

' Check if there are more results available.
fMoreAvailable = objUserSearchResultsEvent.MoreAvailable

' If (fMoreAvailable = TRUE) there might be more results 
' available if the search is narrowed.

' Retrieve the results from the 
' IRTCUserSearchResultsEvent object.
Set objUserSearchCollection = objUserSearchResultsEvent.Results

' Retrieve the number of results returned.
lResultCount = objUserSearchCollection.Count

' Loop through the results that were returned.
For lResultLoopVar = 1 To lResultCount

    ' Retrieve this individual result
    Set objUserSearchResult = objUserSearchCollection.Item(lResultLoopVar)

    strURI =         objUserSearchResult.Value(RTCUSC_URI)
    strDisplayName = objUserSearchResult.Value(RTCUSC_DISPLAYNAME)
    strTitle =       objUserSearchResult.Value(RTCUSC_TITLE)
    strOffice =      objUserSearchResult.Value(RTCUSC_OFFICE)
    strPhone =       objUserSearchResult.Value(RTCUSC_PHONE)
    strCompany =     objUserSearchResult.Value(RTCUSC_COMPANY)
    strCity =        objUserSearchResult.Value(RTCUSC_CITY)
    strState =       objUserSearchResult.Value(RTCUSC_STATE)
    strCountry =     objUserSearchResult.Value(RTCUSC_COUNTRY)
    strEmail =       objUserSearchResult.Value(RTCUSC_EMAIL)

    ' Add necessary code for printing or processing strings here.

    ' Release this individual result
    Set objUserSearchResult = Nothing

Next lResultLoopVar

' Release Collection and Event objects
Set objUserSearchCollection = Nothing
Set objUserSearchResultsEvent = Nothing