Are you a world traveler? ZoneTick is a cool utility that'll help you stay in touch over multiple time zones!
 
Copying and Moving Folders  
Nik Okuntseff  MS Exchange Server Programming 

Copying and Moving Folders

How could we copy or move contents of a whole folder to a different location? The following algorithm may be used:
  • Determine ENTRYID of source folder.
  • Open new parent folder.
  • Use its IMAPIFolder::CopyFolder method supplying source folder entry ID as a parameter.
The only difference between copying and moving would be usage of FOLDER_MOVE flag in the last parameter of the CopyFolder function. The code below (Folders/CopyFolder) copies the contents of the Drafts folder into a new folder named "Copy of Drafts". You can modify this code by adding FOLDER_MOVE flag to make this sample move this folder instead of copying.
 

#include <afxwin.h>
#include <edk.h>

int main()
{
    /*
     * Algorithm:
     *  1. Logon to MAPI and open message store.
     *  2. Locate source folder.
     *  3. Open the parent of the destination folder.
     *  4. Call IMAPIFolder::CopyFolder
     */

    // Change if necessary
    char * pszSourceFolder = "@PR_IPM_SUBTREE_ENTRYID\\Drafts";
    char * pszNewParentFolder = "@PR_IPM_SUBTREE_ENTRYID";
 
    HRESULT hr;
    LPMAPISESSION pSession = NULL;

    // Initialize MAPI
    hr = MAPIInitialize(NULL);
    if (FAILED(hr)) throw -1;

    // Obtain MAPI session
    hr = MAPILogonEx(0,    // Handle to parent window
      "MS Exchange Settings", // Profile name
      NULL,   // Password
      MAPI_NEW_SESSION |
      MAPI_EXTENDED |
      MAPI_LOGON_UI, // Logon flags
      &pSession);  // Resulting MAPI session
    if (FAILED(hr)) throw -1;

    // Find default message store
    ULONG cbDefStoreEid = 0;
    LPENTRYID pDefStoreEid = NULL;
    hr = HrMAPIFindDefaultMsgStore(pSession, &cbDefStoreEid, &pDefStoreEid);
    if (FAILED(hr)) throw -1;

    // Open default message store
    LPMDB pDefMsgStore = NULL;
    hr = pSession->OpenMsgStore(0, cbDefStoreEid, pDefStoreEid, NULL,
        MDB_WRITE | MAPI_DEFERRED_ERRORS, &pDefMsgStore);
    if (FAILED(hr)) throw -1;

    // Locate source folder
    ULONG cbSourceEid = 0;
    LPENTRYID pSourceFolderEid = NULL;
    hr = HrMAPIFindFolderEx(pDefMsgStore,
       '\\',
       pszSourceFolder,
       &cbSourceEid,
       &pSourceFolderEid);
    if (FAILED(hr)) throw -1;

    // Open the parent of the destination folder
    LPMAPIFOLDER pNewParentFolder = NULL;
    hr = HrMAPIOpenFolderEx(pDefMsgStore,
       '\\',
       pszNewParentFolder,
       &pNewParentFolder);
    if (FAILED(hr)) throw -1;

    // Copy the folder
    hr = pNewParentFolder->CopyFolder(cbSourceEid,
          pSourceFolderEid,
          NULL,
          pNewParentFolder,
          "Copy of Drafts",
          NULL,
          NULL,
          COPY_SUBFOLDERS); // || FOLDER_MOVE
    if (FAILED(hr)) throw -1;

    // Cleanup
    if (pNewParentFolder)
        pNewParentFolder->Release();
    if (pSourceFolderEid)
        MAPIFreeBuffer(pSourceFolderEid);
    if (pDefMsgStore)
        pDefMsgStore->Release();
    if (pDefStoreEid)
        MAPIFreeBuffer(pDefStoreEid);
    if (pSession)
        pSession->Release();
    ::MAPIUninitialize();
    return 0;
}
 

[ Contents | Home ]

Send comments and suggestions to niko@wrconsulting.com
Copyright © 1997-1998 by Nik Okuntseff