Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Search and List Recipients Using IDirectorySearch


View products that this article applies to.

This article was previously published under Q223151

↑ Back to the top


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.

↑ Back to the top


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;
}
				

↑ Back to the top


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:

↑ Back to the top


Keywords: KB223151, kbmsg, kbhowto

↑ Back to the top

Article Info
Article ID : 223151
Revision : 5
Created on : 9/28/2007
Published on : 9/28/2007
Exists online : False
Views : 589