Simple Query

Topic Last Modified: 2006-06-12

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.

Example

VBScript

Example

'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