Here's a short article describing how you can use the Exchange Web Services (EWS) FindItem operation to list the ten most recent messages in another user's Inbox that have a certain substring in the subject. Naturally, you need to have the required permissions within the mailbox. This code was developed on Exchange 2007, but will no doubt also function with Exchange 2010 when it appears.
The article is a simple demonstration of how you can use the FindItem operation to search for specific messages. The output is then displayed using XSL. Copy the code into an .htm file, and then open it in Internet Explorer. Change the http to https if necessary, supply the name of your Exchange server, enter the smtp address of the other mailbox (if you don't, it will search your own). Enter a search term (if required), and click the button. It may ask you to login.
Hopefully, you will be rewarded with a list of the ten most recent messages in the other user's inbox whose subject matches the text you supplied.
If you want to search the message body, instead of just the subject, change the bit that says message:Subject to message:Body
<html>
<head>
<script language="VBScript">
Dim objXMLHTTP, objXMLDoc
Sub getMessages_OnClick()
strProtocol = document.all.protocol.value
strServerName = document.all.server.value
strEmail = document.all.email.value
strSearch = document.all.search.value
strURL = strProtocol & "://" & strServername & "/EWS/Exchange.asmx"
Set objXMLHTTP = CreateObject("Microsoft.XMLHTTP")
objXMLHTTP.Open "POST", strURL, True
objXMLHTTP.setRequestHeader "Content-type:", "text/xml"
objXMLHTTP.onReadyStateChange = getRef("checkXMLHTTPState")
strXML = "<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""" & _
" xmlns:m=""http://schemas.microsoft.com/exchange/services/2006/messages""" & _
" xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">" & _
"<soap:Body>" & _
"<m:FindItem Traversal=""Shallow"">" & _
"<m:ItemShape>" & _
"<t:BaseShape>Default</t:BaseShape>" & _
"</m:ItemShape>" & _
"<m:IndexedPageItemView MaxEntriesReturned=""10"" BasePoint=""Beginning"" Offset=""0""/>"
If strSearch <> "" Then
strXML = strXML & _
"<m:Restriction>" & _
"<t:Contains ContainmentComparison=""IgnoreCase"" ContainmentMode=""Substring"">" & _
"<t:FieldURI FieldURI=""item:Subject""/>" & _
"<t:Constant Value=""" & strSearch & """/>" & _
"</t:Contains>" & _
"</m:Restriction>"
End If
strXML = strXML & _
"<m:ParentFolderIds>" & _
"<t:DistinguishedFolderId Id=""inbox"">"
If strEmail <> "" Then
strXML = strXML & "<t:Mailbox><t:EmailAddress>" & strEmail & "</t:EmailAddress></t:Mailbox>"
End If
strXML = strXML & _
"</t:DistinguishedFolderId>" & _
"</m:ParentFolderIds>" & _
"</m:FindItem>" & _
"</soap:Body>" & _
"</soap:Envelope>"
objXMLHTTP.Send(strXML)
End Sub
Sub checkXMLHTTPState
If objXMLHTTP.readyState = 4 Then
responseStatus.innerHTML = objXMLHTTP.Status & " - " & objXMLHTTP.StatusText
Set objXMLDoc = objXMLHTTP.ResponseXML
XSLDiv.innerHTML = objXMLDoc.TransformNode(responseXSL.documentElement)
Set objXMLHTTP = Nothing
Set objXMLDoc = Nothing
End If
End Sub
</script>
<xml id="responseXSL">
<xsl:template xmlns:xsl="uri:xsl" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<xsl:for-each select="//t:Message">
<xsl:value-of select="t:Subject"/><br/>
</xsl:for-each>
</xsl:template>
</xml>
</head>
<body>
<font face="Sans-serif" size="2">
<input name="protocol" value="http" size="5">://<input name="server" value="localhost"><br>
Email:<input name="email"><br>
Search:<input name="search"><br>
<input type="button" name="getMessages" value="GO">
<span id="responseStatus"></span>
<p>
<div id="XSLDiv"></div>
</body>
</html>
Copyright © 2003 - 2012 Lee Derbyshire. All rights reserved.