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 Modify Recipients of Exchange Global Address List


View products that this article applies to.

Summary

This article describes the steps necessary to open the Exchange Global Address List (GAL) for modification. It also describes how to modify a property of a given recipient in an Exchange Global Address List.

↑ Back to the top


More information

The following steps are necessary for modifying a recipient in the GAL:
  1. Open a MAPI session.
  2. Find the entry id (PR_ENTRYID) of the Address Entry to modify.
  3. Call OpenAddressBook from session object.
  4. Open the root container of the Address book by calling IAddrbook::OpenEntry and passing NULL and 0 as the first two parameters.
  5. Call IMAPIContainer::GetHierarchyTable to open the Hierarchy table of the root address book container.
  6. Call HrQueryAllRows using a restriction for the only row that contains DT_GLOBAL as the PR_DISPLAY_TYPE property.
  7. Call IAddrbook::OpenEntry using the PR_ENTRYID from the row returned from HrQueryAllRows to get back an IMAPIContainer object.
  8. Call IMAPIContainer::OpenEntry using the entry id of the recipient you want to modify.
Now that you have the recipient wrapped in an IMAPIProp interface, you can make any modifications to it as long as you have the appropriate permissions.

The following code sample illustrates the above steps.

The following .lib files must be statically linked into your project for this code to compile and link correctly:
  • Mapi32.lib (if building for 32-bit)
  • Mapi.lib (if building for 16-bit)

Sample Code

    #define USES_IID_IABContainer
    #define USES_IID_IMailUser
    #define USES_IID_IAddrBook
    #define INITGUID

    #include <mapix.h>
    #include <mapiguid.h>
    #include <mapidefs.h>
    #include <mapiutil.h>

    HWND g_hWnd = NULL;  // Set g_hWnd to a window handle in your app.
                         // Specifically the window that will be the parent
                         // of the Address Book dialog.

    HRESULT Logon ( LPMAPISESSION *pSession )
    {
       HRESULT hRes = S_OK;
       LPMAPISESSION pLocalSession = NULL;
       ULONG ulFlags = 0L;

       if ( FAILED ( hRes = MAPIInitialize ( NULL ) ) )
          goto Quit;

       ulFlags = MAPI_EXTENDED |
                 MAPI_LOGON_UI |
                 MAPI_EXPLICIT_PROFILE |
                 MAPI_NEW_SESSION;

       if ( FAILED ( hRes = MAPILogonEx ( 0l,
                                          NULL,
                                          NULL,
                                          ulFlags,
                                          &pLocalSession ) ) )
          goto Quit;

       *pSession = pLocalSession;

    Quit:

    // TODO: Add code to release and free all memory allocated by MAPI.
       return hRes;
    }

    HRESULT ModifyGALRecipient( LPMAPISESSION pSession,
                                ULONG cbRecipEID,
                                LPBYTE lpbRecip )
    {

       HRESULT hRes = S_OK;
       ULONG ulFlags = 0L;
       ULONG ulObjType = 0L;
       ULONG cbEID = 0L;
       LPADRBOOK pAddrBook = NULL;
       LPMAPICONTAINER pGAL = NULL;
       LPMAPICONTAINER pIABRoot = NULL;
       LPMAPITABLE pHTable = NULL;
       LPMAILUSER pRecipient = NULL;
       LPSRowSet pRows = NULL;
       SRestriction sres;
       SPropValue spv;
       LPBYTE lpb = NULL;

       ZeroMemory ( (LPVOID) &sres, sizeof (SRestriction ) );

       // - Call OpenAddressBook from session object

       ulFlags = AB_NO_DIALOG;

       if ( FAILED ( hRes = pSession -> OpenAddressBook ( 0L,
                                                          NULL,
                                                          ulFlags,
                                                          &pAddrBook ) ) )
       goto Quit;
       else
       ulFlags = 0L;

       // - Open the root container of the Address book by calling
       //   IAddrbook::OpenEntry and passing NULL and 0 as the first 2
       //   params.

       ulFlags = MAPI_MODIFY;

       if ( FAILED ( hRes = pAddrBook -> OpenEntry ( NULL,
                                               0L,
                                               &IID_IABContainer,
                                               ulFlags,
                                               &ulObjType,
                                               (LPUNKNOWN *) &pIABRoot ) ))
          goto Quit;
       else
          ulFlags = 0L;

       // Make sure we have a Container object
       if ( MAPI_ABCONT == ulObjType )
       {
          // - Call IMAPIContainer::GetHierarchyTable to open the Hierarchy
          //   table of the root address book container.
          if ( FAILED ( hRes = pIABRoot -> GetHierarchyTable ( ulFlags,
                                                            &pHTable ) ) )
             goto Quit;
          else
             ulFlags = 0l;

          // - Call HrQueryAllRows using a restriction for the only row
          //   that contains DT_GLOBAL as the PR_DISPLAY_TYPE property.

          SizedSPropTagArray ( 2, ptaga ) = {2,PR_DISPLAY_TYPE,PR_ENTRYID};

          sres.rt                          = RES_PROPERTY;
          sres.res.resProperty.relop       = RELOP_EQ;
          sres.res.resProperty.ulPropTag   = PR_DISPLAY_TYPE;
          sres.res.resProperty.lpProp      = &spv;

          spv.ulPropTag = PR_DISPLAY_TYPE;
          spv.Value.l   = DT_GLOBAL;

          if ( FAILED ( hRes = HrQueryAllRows ( pHTable,
                                                (LPSPropTagArray) &ptaga,
                                                &sres,
                                                NULL,
                                                0L,
                                                &pRows ) ) )
             goto Quit;
          else
             ulFlags = 0L;

          // - Call IAddrbook::OpenEntry using the entry id (PR_ENTRYID)
          // from the row returned from HrQueryAllRows to get back an
          // IMAPIContainer object.

          ulFlags = MAPI_MODIFY;
          cbEID = pRows->aRow->lpProps[1].Value.bin.cb;
          lpb = pRows->aRow->lpProps[1].Value.bin.lpb;

          if ( FAILED ( hRes = pAddrBook -> OpenEntry ( cbEID,
                                                        (LPENTRYID)lpb,
                                                         NULL,
                                                         ulFlags,
                                                         &ulObjType,
                                                         (LPUNKNOWN *)&pGAL
                                                         ) ) )
             goto Quit;
          else
             ulFlags = 0L;

          // - Call IMAPIContainer::OpenEntry using the entry id of the
          //   recipient you want to modify.

          ulFlags = MAPI_MODIFY;
          ulObjType = 0;

          if ( FAILED ( hRes = pGAL -> OpenEntry ( cbRecipEID,
                                             (LPENTRYID)lpbRecip,
                                             NULL,
                                             ulFlags,
                                             &ulObjType,
                                             (LPUNKNOWN *)&pRecipient ) ) )
             goto Quit;

           // TODO: Replace the following code with modifications you want to make.
           SPropValue spvNewProp;

           spvNewProp.ulPropTag = PR_BUSINESS_TELEPHONE_NUMBER;
           spvNewProp.Value.lpszA = "(123)456-7890";

           hRes = pRecipient->SetProps(1, &spvNewProp, NULL);
           hRes = pRecipient->SaveChanges(FORCE_SAVE);
           // END TODO
        }
        else
           goto Quit;

    Quit:

    // TODO: Add code to release and free all memory allocated by MAPI

       return hRes;
    }


    HRESULT ModifyRecipient ( )
    {
       HRESULT hRes = S_OK;
       ULONG ulFlags = 0L;
       LPMAPISESSION pSession = NULL;
       LPADRBOOK pAddrBook = NULL;
       ADRPARM AdrParm;
       LPADRLIST pAddrList = NULL;
       ULONG cbEID = 0L;
       LPBYTE lpb = NULL;

       ZeroMemory ( (LPVOID) &AdrParm, sizeof ( ADRPARM ) );

       // set up the adrparm struct for Address
       AdrParm.ulFlags             = DIALOG_MODAL | AB_RESOLVE;
       AdrParm.cDestFields         = 1;
       AdrParm.nDestFieldFocus      = 0;
       AdrParm.lpszNewEntryTitle  = "Whatever";
       AdrParm.lpszDestWellsTitle = "";

       AdrParm.lpszCaption = "Browse Address Book";

       // - Open a MAPI session and pick an Address Entry to modify

       // Logon to MAPI session
       if ( FAILED ( hRes = Logon ( &pSession ) ) )
          goto Quit;

       // Pick a recipient

       if ( FAILED ( hRes = pSession -> OpenAddressBook ( 0L,
                                                          NULL,
                                                          ulFlags,
                                                          &pAddrBook ) ) )
          goto Quit;

       if ( FAILED ( hRes = pAddrBook -> Address ( (ULONG *) &g_hWnd,
                                                   &AdrParm,
                                                   &pAddrList) ) )
          goto Quit;

       cbEID = pAddrList->aEntries[0].rgPropVals[2].Value.bin.cb;
       lpb = pAddrList->aEntries[0].rgPropVals[2].Value.bin.lpb;

       // Modify the recipient

       if ( FAILED ( hRes = ModifyGALRecipient ( pSession, cbEID, lpb ) ) )
          goto Quit;

    Quit:
    // TODO: Add code to release and free all memory allocated by MAPI.
       return hRes;
    }
				

↑ Back to the top


Keywords: KB197191, kbmsg, kbhowto, kbcode, kbarchive, kbnosurvey

↑ Back to the top

Article Info
Article ID : 197191
Revision : 7
Created on : 2/21/2014
Published on : 2/21/2014
Exists online : False
Views : 630