Moving/Copying Objects Using CDOEX
Moving/Copying Objects Using CDOEX
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.
// Move/Copy objects using CDOEX // 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 CopyMoveObjects(_bstr_t strDomainName, _bstr_t strLocalPathOfSourceObject, _bstr_t strLocalPathOfDestFolder, bool bOption); HRESULT GetDomainName(BSTR * bstrDomainName); void main(void) { _bstr_t strDomainName; _bstr_t strLocalPathOfSourceObject; _bstr_t strLocalPathOfDestFolder; _bstr_t strUser; HRESULT hr = S_OK; BSTR bstrDomainDNSName; // Get your domain name. hr = GetDomainName(&bstrDomainDNSName); strDomainName = (_bstr_t)bstrDomainDNSName; // TODO strUser = "user1"; // Sample 1: Move files in MBX. strLocalPathOfSourceObject = "MBX/" + strUser + "/Outbox/TestFolder/Hello.txt"; strLocalPathOfDestFolder = "MBX/" + strUser + "/Deleted Items"; CopyMoveObjects(strDomainName, strLocalPathOfSourceObject, strLocalPathOfDestFolder, true); // Sample 2: Copy folders in MBX. strLocalPathOfSourceObject = "MBX/" + strUser + "/Outbox/TestFolder"; strLocalPathOfDestFolder = "MBX/" + strUser + "/Deleted Items"; CopyMoveObjects(strDomainName, strLocalPathOfSourceObject, strLocalPathOfDestFolder, false); // Sample 3: Move folders in Public Folders. strLocalPathOfSourceObject = "Public Folders/SourceFolder"; strLocalPathOfDestFolder = "Public Folders/DestFolder"; CopyMoveObjects(strDomainName, strLocalPathOfSourceObject, strLocalPathOfDestFolder, true); } // CopyMove Objects // bOption: // True : move // False: copy void CopyMoveObjects(_bstr_t strDomainName, _bstr_t strLocalPathOfSourceObject, _bstr_t strLocalPathOfDestFolder, bool bOption) { _ConnectionPtr conn(__uuidof(Connection)); _RecordPtr Rec(__uuidof(Record)); _bstr_t strSourceObjectUrl; _bstr_t strDestFolderUrl; _bstr_t strObjectName; HRESULT hr = S_OK; try { // Set the URL to the location of the source folder. strSourceObjectUrl = "file://./backofficestorage/" + strDomainName + "/" + strLocalPathOfSourceObject; printf("strSourceObjectUrl = %s\n", (char *)strSourceObjectUrl); // Open the connection. conn->Provider = "Exoledb.DataSource"; hr = conn->Open(strSourceObjectUrl,"","",0); // Open the record. hr = Rec->Open((_variant_t)strSourceObjectUrl, conn->ConnectionString, adModeReadWrite, adFailIfNotExists, adOpenSource, bstr_t(), bstr_t()); strObjectName = (_bstr_t)Rec->Fields->GetItem((_variant_t)"DAV:displayname")->Value; // Set the URL to the location of the destination folder. strDestFolderUrl = "file://./backofficestorage/" + strDomainName + "/" + strLocalPathOfDestFolder; if (bOption) // Move the folder from the source location to the destination folder. Rec->MoveRecord(strSourceObjectUrl, strDestFolderUrl + "/" + strObjectName, "", "", adMoveOverWrite, false); else // Copy the folder from the source location to the destination folder. Rec->CopyRecord(strSourceObjectUrl, strDestFolderUrl + "/" + strObjectName, "", "", adCopyOverWrite, false); // Close the connections. Rec->Close(); conn->Close(); // Clean up. Rec = NULL; conn = NULL; printf("Good Job!\n"); } 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.