HOWTO: Getting the Contents of the Exchange Global Address List

Last reviewed: January 7, 1998
Article ID: Q166106
The information in this article applies to:
  • Extended Messaging Application Programming Interface (MAPI), version 1.0
  • Exchange Software Development Kit (SDK)

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          : EDKAPI EMAPI
Version           : 1.0
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 7, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.