Share via


Deleting Searched Objects Using CDOEX and ADO

Deleting Searched Objects Using CDOEX and 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.

// Delete Searched Objects Using Recordset and CDO
// Modify the code where you see TODO and make sure that 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 DeleteSearchedObjects(_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;
 
    // TODO
    strUser = "user1";   
    
    // Sample 1: Delete the appointments with the subject "Test Appointment" 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";
    
    // TODO
    strSearchSql = strSearchSql + " AND \"urn:schemas:httpmail:subject\" = 'Test Appointment'";   
    
    DeleteSearchedObjects(strDomainName, strLocalPathOfSourceFolder, strSearchSql);
    
    
    // Sample 2: Delete the Hello.txt file from the "Deleted Items" folder.
    strLocalPathOfSourceFolder = "MBX/" + strUser + "/Deleted Items";   
    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 + ", \"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";

    // TODO
    strSearchSql = strSearchSql + " AND \"DAV:displayname\" = 'Hello.txt'";   
     
    DeleteSearchedObjects(strDomainName, strLocalPathOfSourceFolder, strSearchSql);
    
    
    // Sample 3: Delete the "TestFolder" subfolder from "Public Folders".
    strLocalPathOfSourceFolder = "Public Folders"; 
    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 + ", \"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";

    // TODO
    strSearchSql = strSearchSql + " AND \"DAV:displayname\" = 'TestFolder'";    
 
    
    DeleteSearchedObjects(strDomainName, strLocalPathOfSourceFolder, strSearchSql);
         
}

// Delete Searched Objects Using the Recordset
void DeleteSearchedObjects(_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 URL to the location of the folders.
              strSourceFolderUrl = "file://./backofficestorage/" + strDomainName + "/" + strLocalPathOfSourceFolder;

      // Open the connection.
      conn->Provider = "Exoledb.DataSource";   
      hr = conn->Open(strSourceFolderUrl,"","",0);
      
      if (conn->State == adStateOpen)
         printf("Connection Opened!\n");
      else
         printf("Connection failed!\n");
      
      // Open the recordset.
      hr = Rst->Open(strSearchSql,conn->ConnectionString,adOpenForwardOnly,adLockOptimistic,0);  

      // Test to see if the recordset opened without error.
      if (Rst->State == adStateOpen)
         printf("Rst Opened Successfully!\n");
      else
         printf("Rst Opened Failed!\n");

      // Were any objects found?
      if (Rst->RecordCount == 0)
      {
                     printf("No object has been found!\n");
         return;
      }
           

      // Objects have been found.
      hr = Rst->MoveFirst();

      _bstr_t strResult = "";
      while (VARIANT_FALSE == Rst->adoEOF)
      {

                     // Delete the current one.
                     Rst->Delete(adAffectCurrent);  

         // You can also make further restrictions to the objects to be deleted,
         // such as deleting an object with a specific display name.
         _bstr_t strObjectName;
 
         // Retrieve the display name of the object.
         FieldsPtr Flds = Rst->GetFields(); 
         FieldPtr Fld = Flds->GetItem("DAV:displayname");  
         strObjectName = (_bstr_t)(Fld->Value);

         printf("strObjectUrl : %s\n", (char *)strObjectName);

                     // TODO
                     if (wcsicmp(strObjectName, (_bstr_t)"Hello.txt") == 0)

            // Delete the current one.
            Rst->Delete(adAffectCurrent);
         */

         Rst->MoveNext();
      }

      // Close the connections.
      Rst->Close();
      conn->Close();

      // Clear up.
          Rst = 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.