HOWTO: Getting the Contents of the Exchange Global Address List

ID: Q166106

The information in this article applies to:

SUMMARY

This article describes the process and code for opening the Microsoft Exchange Global Address List (GAL) and then retrieving the list of recipients in it. This code is specific to Microsoft Exchange Server. Any other MAPI compliant server will have a similar process but the actual code may be different.

MORE INFORMATION

To find the global address list:

1. Start a MAPI session.

2. Open the address book by calling IMAPISession::OpenAddressBook, passing

   the interface identifier (IID) of the address book object. This call
   returns an lppAdrBook parameter, which points to a variable where the
   pointer to the returned address book object is stored.

3. Call HrFindExchangeGlobalAddressList to obtain the entry identifier
   (lppeid) of the global address list container within the address book.

4. Call the OpenEntry function, passing the entry identifier returned in
   step 2.

Following is the code for opening the Exchange GAL and storing its contents in an SRowSet structure. This code assumes that steps 1 and 2 above have already been completed:

    HRESULT GetGALContents ( LPSRowSet * ppRow )
    {

       ULONG    cbeid    = 0L;
       LPENTRYID   lpeid    = NULL;
       HRESULT     hRes     = S_OK;
       LPSRowSet   pRow;
       ULONG    ulObjType;
       ULONG    cRows    = 0L;

       LPMAPITABLE    lpContentsTable = NULL;
       LPMAPICONTAINER   lpGAL = NULL;

       SizedSPropTagArray ( 2, sptCols ) = { 2,
                        PR_ENTRYID,
                        PR_DISPLAY_NAME };

       if ( FAILED ( hRes = HrFindExchangeGlobalAddressList ( m_pAddrBook,
                                     &cbeid,
                                     &lpeid ) ) )
                goto Quit;

      if(FAILED(hRes = m_pAddrBook->OpenEntry((ULONG) cbeid,
                                         (LPENTRYID) lpeid,
                                         NULL,
                                         MAPI_BEST_ACCESS,
                                         &ulObjType,
                                         (LPUNKNOWN *)&lpGAL)))
               goto Quit;

     if ( ulObjType != MAPI_ABCONT )
            goto Quit;

     if(FAILED(hRes = lpGAL->GetContentsTable(0L, &lpContentsTable)))
                goto Quit;

   if(FAILED(hRes = lpContentsTable->SetColumns((LPSPropTagArray)&sptCols,
                               TBL_BATCH)))
               goto Quit;

    if ( FAILED ( hRes = lpContentsTable -> GetRowCount ( 0, &cRows ) ) )
               goto Quit;

    if ( FAILED ( hRes = lpContentsTable -> QueryRows ( cRows, 0L, &pRow )
       goto Quit;
    else
       *ppRow = pRow;

   Quit:
       if ( NULL != lpGAL)
       {
                 lpGAL -> Release ( );
                 lpGAL = NULL;
       }

       if ( lpContentsTable )
       {
            lpContentsTable -> Release ( );
            lpContentsTable = NULL;
       }

       return hRes;
   }
   // End code

NOTE: In order to compile and run this code, you must have the Win32 SDK and the Platform SDK. The latest versions of these SDK's are January 1997 and March 1997, respectively. The following libraries must be included for linking: Mapi32.lib, Edkmapi.lib, Edkutils.lib, and Addrlkup.lib. If you are building in debug mode, Edkdebug.lib is also necessary. Also, the debug or release MultiThreaded DLL option must be selected in the code generation options of the C/C++ tab in the build settings dialog box in order to link properly.

REFERENCES

For more information on starting a MAPI session read the topic "Opening an Information Store" found on the January 1997 MSDN CD.

Keywords          : kbAPI kbEDK kbMsg kbMAPI100 
Version           : 1.0
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: January 9, 1998