HOWTO: Create, Move, or Rename a Folder Using MAPI

Last reviewed: February 3, 1998
Article ID: Q180232
The information in this article applies to:
  • Extended Messaging Application Programming Interface (MAPI), version 1.0

SUMMARY

From time to time you may need to create, move, or rename a folder programmatically. This is done primarily through the IMAPIFolder and the IMAPIProp interfaces. The code example below walks you through the process of creating, moving, and renaming a folder.

MORE INFORMATION

General Steps (Applies to all three tasks)

  1. Create a MAPI Session.

  2. Get a pointer to the message store.

    NOTE: While the example below uses the private information store, the same logic and code can be used against the public information store.

  3. Create, move, or rename a folder.

  4. Log off and Release the session.

Creating a Folder

  1. Create a pointer to the parent of the new folder. For example, if the new folder should be a subfolder to the mailbox, create a pointer to the top of the information store. If the new folder should be a subfolder of the Inbox, create a pointer to the Inbox.

  2. Using the IMAPIFolder::CreateFolder, add the folder to the folder hierarchy.

Moving a Folder

  1. Create a pointer to the folder that becomes the new parent of the folder being moved.

  2. Using HrMAPIFindFolderEx(), retrieve the count of bytes and entry id of the folder you wish to move.

  3. IMAPIFolder::CopyFolder with the entry id and count of bytes returned in the step above and FOLDER_MOVE in the ulFlags parameter moves the folder.

Renaming a folder

  1. Using HrMAPIFindFolderEx(), retrieve the count of bytes and entry id of the folder you wish to rename.

  2. Open the folder so that the properties of the folder can be changed.

  3. Use HrSetOneProp() to change the PR_DISPLAY_NAME of the folder.

Code Example

The code example below demonstrates these three actions:

  • Creating a folder.
  • Moving a folder.
  • Renaming a folder.

    The additional library files required to compile the code are:

  • Edkguid.lib
  • Addrlkup.lib
  • Edkutils.lib
  • Edkdebug.lib
  • Version.lib
  • Msvcrt.lib
  • Mapi32.lib
  • Edkmapi.lib

       /**********************  Begin Code Example **************** /
       #include <Windows.h>
       #include <edk.h>
       #include <stdio.h>
    
       int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pszCmd,
                          int nCmdShow)
       {
         ULONG         cbEIDStore = 0;
         LPENTRYID      lpEIDStore = NULL;
         ULONG         cbEIDFolder = 0;
         LPENTRYID      lpEIDFolder = NULL;
         LPMAPISESSION   lpSession =   NULL;
         LPMDB         lpStore = NULL;
         LPMAPIFOLDER   lpFolder = NULL;
         LPMAPIFOLDER   lpNewFolder = NULL;
         LPMAPIFOLDER   lpDestFolder = NULL;
         HRESULT         hr = NULL;
         ULONG         ulUIParam =   0;
         SPropValue      spvMsg;
         LPCIID         lpInterface = NULL;
         ULONG         ulFlags = MAPI_BEST_ACCESS;
         ULONG         ulObjType = 0;
          hr = MAPIInitialize(NULL);
         if (FAILED(hr))
         {
            MessageBox(NULL,"MAPIInitialize failed",NULL,MB_OK);
            return 1;
         }
         hr = MAPILogonEx(0, "", NULL,
                    MAPI_LOGON_UI | MAPI_NEW_SESSION |  MAPI_EXTENDED |
                    MAPI_NO_MAIL ,
                    &lpSession);
          if (FAILED(hr))
         {
            MessageBox(NULL,"MAPI Logon failed",NULL,MB_OK);
            goto cleanup;
         }
          hr = HrMAPIFindDefaultMsgStore(lpSession, &cbEIDStore, &lpEIDStore);
          if (FAILED(hr))
         {
            MessageBox(NULL,"Message Store Not Found",NULL,MB_OK);
            goto cleanup;
         }
          hr = lpSession->OpenMsgStore(ulUIParam, cbEIDStore,
                                      lpEIDStore, lpInterface,
                                      ulFlags, &lpStore);
          if (FAILED(hr))
         {
            MessageBox(NULL,"Message Store Not Opened",NULL,MB_OK);
            goto cleanup;
         }
          hr = HrMAPIOpenFolderEx(lpStore, '\\',
                            "\\Top of Information Store\\Inbox",
                            &lpFolder);
          if (FAILED(hr))
         {
            MessageBox(NULL,"Folder Not Opened",NULL,MB_OK);
            goto cleanup;
         }
          //  Creates new folder under the Inbox.
          hr = lpFolder->CreateFolder(FOLDER_GENERIC, "Created Folder",
                                     "Folder Comment", NULL,
                                     OPEN_IF_EXISTS,
                                     &lpNewFolder);
          if (FAILED(hr))
         {
            MessageBox(NULL,"Folder Not Created",NULL,MB_OK);
            goto cleanup;
         }
          // Moves the folder to the main folder tree.
          hr = HrMAPIOpenFolderEx(lpStore, '\\',
                                "\\Top of Information Store",
                                &lpDestFolder);
          if (FAILED(hr))
         {
             MessageBox(NULL,
                  "Top of Information Store Not Opened",NULL,MB_OK);
             goto cleanup;
         }
          hr = HrMAPIFindFolderEx(lpStore, '\\',
                        "\\Top of Information Store\\Inbox\\Created Folder",
                        &cbEIDFolder,
                        &lpEIDFolder);
          if (FAILED(hr))
         {
            MessageBox(NULL,"Folder Not Found",NULL,MB_OK);
            goto cleanup;
         }
          hr = lpFolder->CopyFolder(  cbEIDFolder, lpEIDFolder, NULL,
                                    lpDestFolder, NULL,
                                    NULL,
                                    NULL,
                                    FOLDER_MOVE | COPY_SUBFOLDERS);
          //  Finds folder so that it can be opened and renamed.
          hr = HrMAPIFindFolderEx(lpStore, '\\',
                        "\\Top of Information Store\\Created Folder",
                        &cbEIDFolder, &lpEIDFolder);
          if (FAILED(hr))
         {
            MessageBox(NULL,"Folder Not Found",NULL,MB_OK);
            goto cleanup;
         }
          hr = lpStore->OpenEntry(cbEIDFolder, lpEIDFolder,
                                 NULL, MAPI_BEST_ACCESS,
                                 &ulObjType,
                                 (LPUNKNOWN FAR *)&lpFolder);
          if (FAILED(hr))
         {
            MessageBox(NULL,"Folder Could Not Be Opened",NULL,MB_OK);
            goto cleanup;
         }
          spvMsg.ulPropTag = PR_DISPLAY_NAME;
          spvMsg.Value.lpszA = "Renamed Folder";
    
          hr = HrSetOneProp(lpFolder, &spvMsg);
    
          if (FAILED(hr))
          {
             MessageBox(NULL,"Folder Could Not Be Renamed",NULL,MB_OK);
             goto cleanup;
          }
    
        cleanup:
    
          if (lpSession)
          {
             lpSession->Logoff(0, 0, 0);
             ULRELEASE(lpSession);
          }
          MAPIUninitialize();
          return 0;
        }
    
    
    Keywords          : EMAPI
    Version           : WINDOWS: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: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.