How To Search and List Recipients Using IDirectorySearch (223151)



The information in this article applies to:

  • Microsoft Active Directory Service Interfaces 2.5
  • Microsoft Active Directory Service Interfaces 2.0
  • Microsoft Exchange Server 5.5 SP1
  • Microsoft Exchange Server 5.5 SP2

This article was previously published under Q223151

SUMMARY

There are several ways to find and list Microsoft Exchange Server recipients. If you are looking for a way to perform requests with little demand on the system, the IDirectorySearch interface of Active Directory Services Interfaces (ADSI) is the best option.

MORE INFORMATION

IDirectorySearch is a COM interface that enables you to query a directory server, such as a Microsoft Exchange Server, directly from non-automation clients. The following example shows how to search for a subset of mailboxes on an Exchange Server site.

Sample Code

/*
   include:
      activeds.h
   link with:
      activeds.lib
      adsiid.lib
*/ 

HRESULT MyFunc()
{
   HRESULT hr;
   IDirectorySearch *pSearch;
    
   // Inititalization

   CoInitialize(NULL);

    // Bind to the base search object

   hr = ADsGetObject(
            L"LDAP://server/cn=recipients,ou=site,o=org",
            IID_IDirectorySearch,
            (void**) &pSearch
            );

   if (!SUCCEEDED(hr))
   {
       return hr;
   }
    
   // Perform a subtree search

   ADS_SEARCHPREF_INFO prefInfo[1];
   prefInfo[0].dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE;
   prefInfo[0].vValue.dwType = ADSTYPE_INTEGER;
   prefInfo[0].vValue.Integer = ADS_SCOPE_SUBTREE;
   hr = pSearch->SetSearchPreference( prefInfo, 1);

   // Prepare for attributes to be returned
  
   LPWSTR pszAttr[] = { L"cn",L"title",L"mail"};
   ADS_SEARCH_HANDLE hSearch;
   DWORD dwCount= sizeof(pszAttr)/sizeof(LPWSTR);

   // Search for mailboxes with First Name starting with letter 'F'
  
   hr = pSearch->ExecuteSearch(
             L"(&(objectClass=organizationalPerson)(givenName=F*))",
             pszAttr,
             dwCount,
             &hSearch
             );

   if (!SUCCEEDED(hr))
   {
       pSearch->Release();
       return hr;
   }
   
   // Enumerate the search result 

   ADS_SEARCH_COLUMN col;
   while( pSearch->GetNextRow(hSearch) != S_ADS_NOMORE_ROWS )
   {
      // Print list of attributes
      for(unsigned int i=0; i < dwCount; i++)
      {
          hr = pSearch->GetColumn( hSearch, pszAttr[i], &col );
          if ( SUCCEEDED(hr) )
          {
             printf("\n%S",(LPWSTR)col.pADsValues->CaseIgnoreString);
             pSearch->FreeColumn( &col );
          }
      }
   }
	
   // Clean-up

   pSearch->CloseSearchHandle(hSearch);
   pSearch->Release();

   CoUninitialize();

   return S_OK;
}
				

REFERENCES

For additional information about using ADO for searching , please click the article number below to view the article in the Microsoft Knowledge Base:

187529 How To Using ADO to Access Objects Through ADSI LDAP Provider [vbwin]

For additional information on using ADSI, please see the following Web site:

Modification Type:MinorLast Reviewed:7/13/2004
Keywords:kbhowto kbMsg KB223151