Creating a Folder
| Nik Okuntseff |
MS Exchange Server Programming |
Creating a Folder
How can we create a new folder? We can use the IMAPIFolder::CreateFolder
method of its parent. The following sample (Folders/CreateFolder) illustrates
that:
#include <afxwin.h>
#include <edk.h>
int main()
{
/*
* Algorithm:
* 1. Logon to MAPI and open message
store.
* 2. Open parent folder.
* 3. Call the IMAPIFolder::CreateFolder
using the parent.
*/
// Change if necessary
char * pszParentFolder = "@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;
// Open the desired folder
LPMAPIFOLDER pParentFolder = NULL;
hr = HrMAPIOpenFolderEx(pDefMsgStore,
'\\',
pszParentFolder,
&pParentFolder);
if (FAILED(hr)) throw -1;
// Create new folder
LPMAPIFOLDER pNewFolder = NULL;
hr = pParentFolder->CreateFolder(FOLDER_GENERIC,
"MyFolder", // Display name
NULL, // Comment
NULL,
OPEN_IF_EXISTS,
&pNewFolder);
if (FAILED(hr)) throw -1;
// Cleanup
if (pNewFolder)
pNewFolder->Release();
if (pParentFolder)
pParentFolder->Release();
if (pDefMsgStore)
pDefMsgStore->Release();
if (pDefStoreEid)
MAPIFreeBuffer(pDefStoreEid);
if (pSession)
pSession->Release();
::MAPIUninitialize();
return 0;
}
Notice that I have used the OPEN_IF_EXISTS flag when calling the IMAPIFolder::CreateFolder
function. This forces it to open an existing folder if one already exists
instead of failing.
[ Contents |
Home
]
Send comments and suggestions to niko@wrconsulting.com
Copyright © 1997-1998 by Nik Okuntseff
|