Creating Outlook Folders Using ADO

Creating Outlook Folders Using ADO

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Visual C++

Note  The following example uses a file URL with the Exchange OLE DB (ExOLEDB) provider. The ExOLEDB provider also supports The HTTP: URL Scheme. Using The HTTP: URL Scheme allows both client and server applications to use a single URL scheme.

//Create Special Folders in Outlook Using ADO
//Modify the code where you see TODO and make sure the objects or properties exist.

#include <activeds.h>
#include <stdio.h>
#include <conio.h>
#import <msado15.dll> no_namespace rename("EOF","adoEOF")
#import <cdoex.dll> no_namespace


struct StartOle {
   StartOle() { CoInitialize(NULL); }
   ~StartOle() { CoUninitialize(); }
} _inst_StartOle;


void dump_com_error(_com_error &e)
{
    printf("Oops - hit an error!\n");
    printf("\tCode = %08lx\n", e.Error());
    printf("\tCode meaning = %s\n", e.ErrorMessage());
    _bstr_t bstrSource(e.Source());
    _bstr_t bstrDescription(e.Description());
    printf("\tSource = %s\n", (LPCTSTR) bstrSource);
    printf("\tDescription = %s\n", (LPCTSTR) bstrDescription);
}


void CreateFolder(_bstr_t strDomainName, _bstr_t strLocalPathOfParentFolder, _bstr_t strNewFolderName, _bstr_t strFolderType);
HRESULT GetDomainName(BSTR * bstrDomainName);


void main(void)
{
    _bstr_t strDomainName;
    _bstr_t strLocalPathOfParentFolder;
    _bstr_t strNewFolderName;
    _bstr_t strUser;

   HRESULT hr = S_OK;
   BSTR bstrDomainDNSName;

   // Get your domain name.
   hr = GetDomainName(&bstrDomainDNSName);

    strDomainName = (_bstr_t)bstrDomainDNSName;

    // TODO
    strUser = "user1";

    // TODO
    strNewFolderName = "TestHelloFolder";

    // Sample 1: Create a folder in "MBX" to hold "AppointmentItem".
    // Specify the path of the folder under which a folder is to be created.
    strLocalPathOfParentFolder = "MBX/" + strUser + "/Deleted Items";

    CreateFolder(strDomainName, strLocalPathOfParentFolder, strNewFolderName, (_bstr_t)"IPF.Appointment");


    // Sample 2: Create a folder in "Public Folders" to hold "ContactItem".
    // Specify the path of the folder under which a folder is to be created.
    strLocalPathOfParentFolder = "Public Folders";

    CreateFolder(strDomainName, strLocalPathOfParentFolder, strNewFolderName, (_bstr_t)"IPF.Contact");

}

// Create all kinds of Outlook folders

// strFolderType            Name

// MailItems                IPF.Note
// ContactItems             IPF.Contact
// AppointmentItems         IPF.Appointment
// NoteItems                IPF.StickyNote
// TaskItems                IPF.Task
// JournalItems             IPF.Journal
void CreateFolder(_bstr_t strDomainName, _bstr_t strLocalPathOfParentFolder, _bstr_t strNewFolderName, _bstr_t strFolderType)
{
   _ConnectionPtr conn(__uuidof(Connection));
   _RecordPtr Rec(__uuidof(Record));
   _bstr_t strFolderUrl;

   HRESULT hr = S_OK;

            try
     {
             // Specify the URL to the folder to be created.
             strFolderUrl = "file://./backofficestorage/" + strDomainName + "/" + strLocalPathOfParentFolder + "/" + strNewFolderName;


      // Open the main mailbox folders under MBX.
      conn->Provider = "Exoledb.DataSource";
      hr = conn->Open(strFolderUrl,"","",0);

      hr = Rec->Open((_variant_t)strFolderUrl,conn->ConnectionString,
            adModeReadWrite,
            adCreateCollection,
            adOpenSource,
            bstr_t(), bstr_t());

      // Set the content class.
      FieldsPtr Flds = Rec->GetFields();
      FieldPtr Fld = Flds->GetItem("DAV:contentclass");
              Fld->Value = _variant_t("urn:content-classes:folder");

      // Specify the folder type.
      Fld = Flds->GetItem("https://schemas.microsoft.com/exchange/outlookfolderclass");
              Fld->Value = (_variant_t)strFolderType;

      // Update.
      Flds->Update();

      // Close the connection.
      Rec->Close();
      conn->Close();

      // Clear up.
      Flds = NULL;
      Fld = NULL;
      Rec = NULL;
      conn = NULL;

       printf("Good Job!");

     }
     catch(_com_error &e)
     {
         dump_com_error(e);
     }

}


// GetDomainName
//
// Params:  [out] BSTR * bstrDomainName
// Output:  HRESULT
// Purpose: Retrieve the domain DNS name.
HRESULT GetDomainName(BSTR * bstrDomainName)
{
HRESULT hr = S_OK;
IADsADSystemInfo *pADsys;
hr = CoCreateInstance(CLSID_ADSystemInfo,
NULL,
CLSCTX_INPROC_SERVER,
IID_IADsADSystemInfo,
(void**)&pADsys);
hr = pADsys->get_DomainDNSName(bstrDomainName);

if (pADsys)
pADsys->Release();
return hr;
}

Send us your feedback about the Microsoft Exchange Server 2003 SDK.

Build: June 2007 (2007.618.1)

© 2003-2006 Microsoft Corporation. All rights reserved. Terms of use.