Share via


Traversing Folders (Private and Public) with OLEDB

Traversing Folders (Private and Public) with OLEDB

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.

//Traversing Folders (Private and Public) with OLEDB
//
#include <stdio.h>
#include <conio.h>
#include <activeds.h>

#import <msado15.dll> no_namespace rename("EOF","adoEOF")
#import <cdoex.dll> no_namespace

HRESULT GetDomainName(BSTR * bstrDomainName);


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


void main(void)
{
   _ConnectionPtr conn(__uuidof(Connection));
   _RecordsetPtr MyFolders(__uuidof(Recordset));
   _RecordsetPtr MyPublicFolders(__uuidof(Recordset));
   _RecordPtr MyRecord(__uuidof(Record));
    _bstr_t strDomainName;
   BSTR bstrDomainDNSName;

   HRESULT hr = NULL;
   HRESULT hRes = NULL;

   // Get your Domain Name.
   hr = GetDomainName(&bstrDomainDNSName);
    strDomainName = (_bstr_t)bstrDomainDNSName;

   try
   {
      //Open Private Folders under MBX
      conn->Provider = "Exoledb.DataSource";

      _bstr_t szConnString = "file://./backofficestorage/" + strDomainName + "/mbx/user1";

      hRes = conn->Open(szConnString,"","",0);

      _bstr_t strQ = "select * from scope ('shallow traversal of  \"" + szConnString + "\"') ";

      hRes = MyFolders->Open(strQ,conn->ConnectionString,adOpenForwardOnly,adLockReadOnly,0);

      printf("MailBox\n");

      int reccount = MyFolders->RecordCount;

      MyFolders->MoveFirst();

      FieldsPtr Flds ;
      FieldPtr Fld;
      _bstr_t strName;

      for (int i=0;i<reccount;i++)
      {
         Flds = MyFolders->GetFields();
         Fld = Flds->GetItem("DAV:displayname");
         strName = "----" + (_bstr_t)Fld->Value;
         printf("%s\n", (char *) strName );
         MyFolders->MoveNext();
      }

      conn->Close();
      MyFolders->Close();

      //Open Public Folders
      _bstr_t szPFConnString = "file://./backofficestorage/" + strDomainName + "/Public Folders";

      hRes = conn->Open(szPFConnString,"","",0);

      strQ = "select * from scope ('shallow traversal of  \"" + szPFConnString + "\"') ";

      hRes = MyPublicFolders->Open(strQ,conn->ConnectionString,adOpenForwardOnly,adLockReadOnly,0);

      printf("Public Folders\n");

      reccount = MyPublicFolders->RecordCount;
      MyPublicFolders->MoveFirst();

      for ( i=0;i<reccount;i++)
      {
         Flds = MyPublicFolders->GetFields();
         Fld = Flds->GetItem("DAV:displayname");
         strName = "----" + (_bstr_t)Fld->Value;
         printf("%s\n", (char *) strName);
         MyPublicFolders->MoveNext();
      }

      conn->Close();
      MyPublicFolders->Close();

   }
   catch(...)
   {
      printf("Something failed!\n");

   }
   conn = NULL;
}

/*******************************************
// 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.