Article ID: 180232 - Last Review: August 18, 2005 - Revision: 1.5
How To Create, Move, or Rename a Folder Using MAPI This article was previously published under Q180232
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.
General Steps (Applies to all three tasks) Create a MAPI Session.
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.
Create, move, or rename a folder.
Log off and Release the session.
Creating a Folder 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.
Using the IMAPIFolder::CreateFolder, add the folder to the folder
hierarchy.
Moving a Folder Create a pointer to the folder that becomes the new parent of the folder
being moved.
Using HrMAPIFindFolderEx(), retrieve the count of bytes and entry id of
the folder you wish to move.
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 Using HrMAPIFindFolderEx(), retrieve the count of bytes and entry id of
the folder you wish to rename.
Open the folder so that the properties of the folder can be changed.
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;
}
APPLIES TO Microsoft Office Outlook 2007 Microsoft Messaging Application Programming Interface
Provide feedback on this information
Did this information solve your problem?
Was this information relevant?
What can we do to improve this information?
To protect your privacy, do not include contact information in your feedback.
Thank you! Your feedback is used to help us improve our support content. For more assistance options, please visit the
Help and Support Home Page .