Share via


Create and Send a Search Request

The following code examples demonstrate how to create and send a user search request. The operations in the Initialize RTC code example must be performed before using this example.

Note  This example does not contain error checking or releases appropriate for real code.

C++ Code Example

// Create and initialize the IRTCClient 
// object before using this example.

IRTCUserSearch      *pUserSearch = NULL;
IRTCUserSearchQuery *pUserSearchQuery = NULL;
IRTCProfile         *pIRTCProfile = NULL;

// This is the LDAP attribute used in the search.
BSTR  bstrSearchAttribute = SysAllocString(L"givenName");
// This is the search string used in the search.
BSTR  bstrSearchString = SysAllocString(L"John");
// This is the domain to search against (set to NULL to use the current domain).
BSTR  bstrSearchDomain = NULL;
// This is the maximum time to wait for a response (range: 1-30 seconds).
LONG  lMaxTime = 30;
// This is the maximum number of search results to return (range: 1-1000).
LONG  lMaxMatches = 100;
// This is the cookie for the tracking request.
LONG  lCookie = 1; 
// This is the HRESULT for receiving errors.
HRESULT  hr = 0;

// Query for the UserSearch object.
hr = pIRTCClient->QueryInterface(IID_IRTCUserSearch, 
                                 (void **)&pUserSearch);

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

// Create the UserSearch query object.
hr = pUserSearch->CreateQuery(&pUserSearchQuery);

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

// Add a search term to the query.
hr = pUserSearchQuery->put_SearchTerm(bstrSearchAttribute,
                                      bstrSearchString);

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

// Set the maximum time.
hr = pUserSearchQuery->put_SearchPreference(RTCUSP_TIME_LIMIT,
                                            lMaxTime);

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

// Set the maximum matches.
hr = pUserSearchQuery->put_SearchPreference(RTCUSP_MAX_MATCHES,
                                            lMaxMatches);

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

// Set the search domain.
hr = pUserSearchQuery->put_SearchDomain(bstrSearchDomain);

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

// Create and enable the profile. For more information on
// creating the profile, see the "Create and Enable a Profile" 
// code example.
pIRTCProfile = 	// To create the profile, see the "Create and Enable a Profile" code example.

//Send the search request.
hr = pUserSearch->ExecuteSearch( pUserSearchQuery, 
                                 pIRTCProfile, 
                                 lCookie );

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

// Release the IRTCUserSearch and 
// IRTCUserSearchQuery objects.

Visual Basic Code Example

'Create and initialize the IRTCClient object 
'before using this example.

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

Dim objUserSearch       As IRTCUserSearch
Dim objUserSearchQuery  As IRTCUserSearchQuery
Dim objProfile          As IRTCProfile

Dim strSearchAttribute  As String
Dim strSearchString     As String
Dim strSearchDomain     As String
Dim lMaxTime            As Long
Dim lMaxMatches         As Long
Dim lCookie             As Long

'This is the LDAP attribute used in the search.
strSearchAttribute = "givenName"
'This is the search string used in the search.
strSearchString = "John"
'This is the domain to search against (leave blank to use the current domain).
strSearchDomain = ""
'This is the maximum time to wait for a response (range: 1-30 seconds).
lMaxTime = 30
'This is the maximum number of search results to return (range: 1-1000).
lMaxMatches = 100
'This is the cookie for the tracking request.
lCookie = 1

'Get the UserSearch interface from the RTCClient object.
Set objUserSearch = g_objRTCClient

'Create the UserSearch query from the UserSearch object.
Set objUserSearchQuery = objUserSearch.CreateQuery

'Add a search term to query.
objUserSearchQuery.SearchTerm(strSearchAttribute) = strSearchString

'Set the maximum time.
objUserSearchQuery.SearchPreference(RTCUSP_TIME_LIMIT) = lMaxTime

'Set the maximum number of matches.
objUserSearchQuery.SearchPreference(RTCUSP_MAX_MATCHES) = lMaxMatches

'Set the search domain.
objUserSearchQuery.SearchDomain = strSearchDomain

'Create and enable the profile. For more information on
'creating the profile, see the "Create and Enable a Profile" 
'code example.
Set objProfile =  'Create the profile here.

'Send the search request.
objUserSearch.ExecuteSearch objUserSearchQuery, objProfile, lCookie

'Release the IRTCUserSearch and 
'IRTCUserSearchQuery objects.