Share via


Getting a List of Folders

Getting a List of Folders

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.

This example demonstrates how to get a list of folders within a folder. When constructing the Structured Query Language (SQL) query, you must require that the DAV:isfolder property is True and the DAV:ishidden property is False in the WHERE clause. For example:

select "DAV:displayname", "DAV:contentclass", "DAV:href" from
scope('shallow traversal of "/myfolders/myfolder"')
where "DAV:ishidden" = False AND
      "DAV:isfolder" = True

Note that a relative URL is used in the SCOPE portion of the SQL statement. Searches occur within the context of a connection to a particular public store or private store. Therefore, you need not use the entire URL in the query. The path is relative to the root folder for a public store, and "MBX" for a private store. The beginning forward slash is required. For example, the folder at the URL file://./backofficestorage/domain.tld/Public Folders/myfolders/myfolder can be referred to by using the relative URL /myfolders/myfolder; the path is anchored from the Public Folders root public store folder.

VBScript

 Dim Conn
 Dim sUrl

 Set Conn = CreateObject("ADODB.Connection")
 Conn.Provider = "ExOLEDB.DataSource"
 Conn.Open sUrl

 sUrl = "https://server/public/folder/"

 Dim Rs
 Set Rs = CreateObject("ADODB.Recordset")

 Dim sql
 sql = "select ""DAV:displayname"", ""DAV:contentclass"", ""DAV:href"" from"
 sql = sql & " scope('shallow traversal of """ & sUrl & """')"
 sql = sql & " where ""DAV:ishidden"" = False and ""DAV:isfolder"" = True"

 Rs.Open sql, Conn
 Set GetFolderList = Rs

' Close connection and recordset.
Conn.Close
Rs.Close

Set Conn = Nothing
Set Rs = Nothing

C++

#import <msado15.dll> no_namespace

// Note: It is recommended that all input parameters be validated when they are
// first obtained from the user or user interface.
_RecordsetPtr getFolderList() {

   _ConnectionPtr Conn(__uuidof(Connection));
   _RecordsetPtr Rs(__uuidof(Recordset));
   bstr_t sFolderUrl = "https://server/public/folder/";

   Conn.CreateInstance(__uuidof(Connection));
   Conn->Provider = "ExOLEDB.DataSource";
   try {
      Conn->Open(sFolderUrl, bstr_t(), bstr_t(),-1);
   }
   catch(_com_error e) {

      // Handle error or throw...
      throw e;
   }

   bstr_t sQuery = "select \"DAV:displayname\", \"DAV:contentclass\", \"DAV:href\" from ";
        sQuery += " scope ('shallow traversal of \""+ sFolderUrl + "\"')";
        sQuery += " where \"DAV:ishidden\" = False And \"DAV:isfolder\" = True";

   try {
      Rs->Open(
            variant_t(sQuery),
            variant_t((IDispatch*)Conn, true),
            adOpenUnspecified,
            adLockUnspecified,
            -1);
   }
   catch(_com_error e) {

      // Handle error or throw...
      throw e;
   }

   // Close the connection.
   Conn->Close();
   Conn = NULL;

   return Rs;

}

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.