Searching/Displaying Objects Using CDOEX
Searching/Displaying 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.
//Search and Display objects using CDO and 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 SearchListObjects(_bstr_t strDomainName, _bstr_t strLocalPathOfSourceFolder, _bstr_t strSearchSql); HRESULT GetDomainName(BSTR * bstrDomainName); void main(void) { _bstr_t strDomainName; _bstr_t strLocalPathOfSourceFolder; _bstr_t strSourceFolderUrl; _bstr_t strSearchSql; _bstr_t strUser; HRESULT hr = S_OK; BSTR bstrDomainDNSName; // Get your Domain Name. hr = GetDomainName(&bstrDomainDNSName); strDomainName = (_bstr_t)bstrDomainDNSName; strUser = "user1"; // TODO // Sample 1: List Appointments in 'Calendar'; strLocalPathOfSourceFolder = "MBX/" + strUser + "/Calendar"; strSourceFolderUrl = "file://./backofficestorage/" + strDomainName + "/" + strLocalPathOfSourceFolder; // create the SQL query for the recordset (appointments) strSearchSql = "select "; strSearchSql = strSearchSql + "\"urn:schemas:mailheader:content-class\""; strSearchSql = strSearchSql + ", \"DAV:href\""; strSearchSql = strSearchSql + ", \"DAV:displayname\""; strSearchSql = strSearchSql + ", \"DAV:isfolder\""; strSearchSql = strSearchSql + ", \"DAV:iscollection\""; strSearchSql = strSearchSql + ", \"urn:schemas:httpmail:subject\""; strSearchSql = strSearchSql + " from scope ('shallow traversal of "; strSearchSql = strSearchSql + "\"" + strSourceFolderUrl + "\"" + "')"; strSearchSql = strSearchSql + " WHERE \"DAV:ishidden\" = false"; strSearchSql = strSearchSql + " AND \"DAV:isfolder\" = false"; SearchListObjects(strDomainName, strLocalPathOfSourceFolder, strSearchSql); // Sample 2: List Contacts in 'Ccontacts' strLocalPathOfSourceFolder = "MBX/" + strUser + "/Contacts"; strSourceFolderUrl = "file://./backofficestorage/" + \ strDomainName + "/" + strLocalPathOfSourceFolder; // create the SQL query for the recordset (appointments) strSearchSql = "select "; strSearchSql = strSearchSql + "\"urn:schemas:mailheader:content-class\""; strSearchSql = strSearchSql + ", \"DAV:href\""; strSearchSql = strSearchSql + ", \"DAV:displayname\""; strSearchSql = strSearchSql + ", \"DAV:isfolder\""; strSearchSql = strSearchSql + ", \"DAV:iscollection\""; strSearchSql = strSearchSql + ", \"urn:schemas:httpmail:subject\""; strSearchSql = strSearchSql + " from scope ('shallow traversal of "; strSearchSql = strSearchSql + "\"" + strSourceFolderUrl + "\"" + "')"; strSearchSql = strSearchSql + " WHERE \"DAV:ishidden\" = false"; strSearchSql = strSearchSql + " AND \"DAV:isfolder\" = false"; SearchListObjects(strDomainName, strLocalPathOfSourceFolder, strSearchSql); // Sample 3: Travel all subfolders of 'MBX' under the user strLocalPathOfSourceFolder = "MBX/" + strUser; strSourceFolderUrl = "file://./backofficestorage/" + strDomainName + "/" + strLocalPathOfSourceFolder; // create the SQL query for the recordset (appointments) strSearchSql = "select "; strSearchSql = strSearchSql + "\"urn:schemas:mailheader:content-class\""; strSearchSql = strSearchSql + ", \"DAV:href\""; strSearchSql = strSearchSql + ", \"DAV:displayname\""; strSearchSql = strSearchSql + ", \"DAV:isfolder\""; strSearchSql = strSearchSql + ", \"DAV:iscollection\""; strSearchSql = strSearchSql + ", \"urn:schemas:httpmail:subject\""; strSearchSql = strSearchSql + " from scope ('shallow traversal of "; strSearchSql = strSearchSql + "\"" + strSourceFolderUrl + "\"" + "')"; strSearchSql = strSearchSql + " WHERE \"DAV:ishidden\" = false"; strSearchSql = strSearchSql + " AND \"DAV:isfolder\" = true"; strSearchSql = strSearchSql + " AND \"DAV:iscollection\" = true"; SearchListObjects(strDomainName, strLocalPathOfSourceFolder, strSearchSql); } /******************************** Search objects then display then if found *********************************/ void SearchListObjects(_bstr_t strDomainName, _bstr_t strLocalPathOfSourceFolder, _bstr_t strSearchSql) { _ConnectionPtr conn(_uuidof(Connection)); _RecordsetPtr Rst(_uuidof(Recordset)); _bstr_t strSourceFolderUrl; HRESULT hr = S_OK; try { // set the strURL to the location of the folders strSourceFolderUrl = "file://./backofficestorage/" + strDomainName + "/" + strLocalPathOfSourceFolder; printf("strSourceFolderUrl : %s\n", (char *)strSourceFolderUrl); // open connection conn->Provider = "Exoledb.DataSource"; hr = conn->Open(strSourceFolderUrl,"","",0); if (conn->State == adStateOpen) printf("Connection Opened!\n"); else printf("Connection failed!\n"); // open Recordset hr = Rst->Open(strSearchSql,conn->ConnectionString,adOpenForwardOnly,adLockReadOnly,0); //du test if (Rst->State == adStateOpen) printf("Rst Opened Successfully!\n"); else printf("Rst Opened Failed!\n"); // check if found any objects if (Rst->RecordCount == 0) { printf("No object found\n"); return; } // now found some objects hr = Rst->MoveFirst(); while (VARIANT_FALSE == Rst->adoEOF) { _bstr_t strObjectUrl; _bstr_t strContentClass; bool boolIsCollection; // retrieve some properties FieldsPtr Flds = Rst->GetFields(); FieldPtr Fld = Flds->GetItem("DAV:href"); strObjectUrl = (_bstr_t)(Fld->Value); Fld = Flds->GetItem("urn:schemas:mailheader:content-class"); strContentClass = (_bstr_t)(Fld->Value); Fld = Flds->GetItem("DAV:iscollection"); boolIsCollection = (bool)(Fld->Value); //printf("Content-clase = %s\n", (char *)strContentClass); // consider each situation if (boolIsCollection) { Fld = Flds->GetItem("DAV:displayname"); printf("Folder : %s\n", (char *)(_bstr_t)(Fld->Value)); } else{ if (wcsicmp(strContentClass, (_bstr_t)"urn:content-classes:message") == 0) { IMessagePtr pMsg(__uuidof(Message)); IDataSourcePtr pDsrc; hr = pMsg->get_DataSource(&pDsrc); hr = pDsrc->Open((_bstr_t)strObjectUrl, NULL, adModeReadWrite, adFailIfNotExists, adOpenSource, "", ""); // get the date received Flds = pMsg->GetFields(); Fld = Flds->GetItem("urn:schemas:httpmail:datereceived"); // TODO printf("Message\n"); printf("Sender :%s\n", (char *)(_bstr_t)pMsg->Sender); // TODO printf("TimeReceived :%s\n\n", (char *)(_bstr_t)(Fld->Value)); // clear up pDsrc = NULL; pMsg = NULL; } else if (wcsicmp(strContentClass, (_bstr_t)"urn:content-classes:person") == 0) { IPersonPtr pPerson(__uuidof(Person)); IDataSourcePtr pDsrc; hr = pPerson->get_DataSource(&pDsrc); hr = pDsrc->Open( (_bstr_t)strObjectUrl, NULL, adModeReadWrite, adFailIfNotExists, adOpenSource, "", ""); printf("Person\n"); printf("First Name :%s\n", (char *)(_bstr_t)pPerson->FirstName); // TODO printf("Last Name :%s\n", (char *)(_bstr_t)pPerson->LastName); // TODO printf("Title :%s\n", (char *)(_bstr_t)pPerson->Title); // TODO printf("Company :%s\n\n", (char *)(_bstr_t)pPerson->Company); // TODO // clear up pDsrc = NULL; pPerson = NULL; } else if (wcsicmp(strContentClass, (_bstr_t)"urn:content-classes:appointment") == 0) { IAppointmentPtr pAppt(__uuidof(Appointment)); IDataSourcePtr pDsrc; hr = pAppt->get_DataSource(&pDsrc); hr = pDsrc->Open( (_bstr_t)strObjectUrl, NULL, adModeReadWrite, adFailIfNotExists, adOpenSource, "", ""); printf("Appointment\n"); printf("Subject :%s\n", (char *)(_bstr_t)pAppt->Subject); // TODO printf("Location :%s\n", (char *)(_bstr_t)pAppt->Location); // TODO // retrieve the date using Fields Flds = pAppt->GetFields(); Fld = Flds->GetItem("urn:schemas:calendar:dtstart"); printf("StartTime :%s\n", (char *)(bstr_t)(Fld->Value)); Fld = Flds->GetItem("urn:schemas:calendar:dtend"); printf("EndTime :%s\n\n", (char *)(bstr_t)(Fld->Value)); // clear up pDsrc = NULL; pAppt = NULL; } else { printf("Else case = %s\n", (char *)strContentClass); } } Rst->MoveNext(); } // close connections Rst->Close(); conn->Close(); // clear up Rst = NULL; conn = NULL; } 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.