Simple Query

Simple Query

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.

The following example shows how to construct and run a query on the Exchange store, the results of which are passed to the DoResults function in the Enumerating Search Results.

VBScript

'Finds items from a sender
'Passes search result recordset to DoResults

On Error GoTo ErrHandler

Const adErrNoCurrentRecord = 3021
Dim sender
Dim relURL
Dim Rs
Dim Conn
Dim cName
Dim dName
Dim info
Dim infoNT
Dim strQ

'get computer and domain information
Set info   = CreateObject("ADSystemInfo")
Set infoNT = CreateObject("WinNTSystemInfo")
cName = infoNT.ComputerName
dName = info.DomainDNSName

'create connection object
Set Conn = CreateObject("ADODB.Connection")
Conn.Provider = "Exoledb.DataSource"

'URL for connection object
'is at the virtual directory root
relURL = "http://" & _
      cName & "." & _
      dName & "/" & _
      "public"

Conn.Open relURL

'relative URL is the folder to search
relURL = "Reports"

sender = "Jane Clayton"

Set Rs = CreateObject("ADODB.Recordset")

'construct the SQL query
strQ = "SELECT ""urn:httpmail:subject"" "
strQ = strQ & "FROM """ & relURL & """ "

'* ------
'* A shallow traversal is the default scope. To explicitly
'* specify a shallow traversal, the code would be:
'* 'strQ = strQ & "FROM scope('shallow traversal of """ & strURL & """ ')"
'*-------

strQ = strQ & "WHERE ""urn:schemas:httpmail:sendername"" = '" & sender & "'"

Rs.Open strQ, Conn

'If empty recordset, return error
'If successful call DoResults routine passing the recorset
If Rs.EOF = True Then
    Response.Write "No items found, run another query."
Else
    Response.Write "Success! Found " & Rs.RecordCount
    DoResults Rs
End If
Rs.Close

GoTo Ending

' Implement custom error handling here.
ErrHandler:
   WScript.echo Err.Number + " " + Err.Description
   Err.Clear

Ending:
   ' Clean up.
   Conn.Close
   Rs.Close

   Set Conn = Nothing
   Set Rs = Nothing

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.