Finding Items (Exchange Web Services)
Topic Last Modified: 2007-09-14
You can use Exchange Web Services to find items that are located in specified folders.
Example
The following example shows you how to search for items in the Inbox and Drafts distinguished folders.
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Credentials = new NetworkCredential("UserName", "Password", "Domain");
esb.Url = @"http://ExchangeServer.example.com/EWS/Exchange.asmx";
// Form the FindItem request.
FindItemType findItemRequest = new FindItemType();
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
// Define which item properties are returned in the response.
ItemResponseShapeType itemProperties = new ItemResponseShapeType();
itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;
// Add properties shape to the request.
findItemRequest.ItemShape = itemProperties;
// Identify which folders to search to find items.
DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[2];
folderIDArray[0] = new DistinguishedFolderIdType();
folderIDArray[1] = new DistinguishedFolderIdType();
folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;
folderIDArray[1].Id = DistinguishedFolderIdNameType.drafts;
// Add folders to the request.
findItemRequest.ParentFolderIds = folderIDArray;
try
{
// Send the request and get the response.
FindItemResponseType findItemResponse = esb.FindItem(findItemRequest);
// Get the response messages.
ResponseMessageType[] rmta = findItemResponse.ResponseMessages.Items;
foreach (ResponseMessageType rmt in rmta)
{
// Cast to the correct response message type.
if (((FindItemResponseMessageType)rmt).ResponseClass == ResponseClassType.Success)
Console.WriteLine("Item found");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
The FindItem Operation does not support deep traversal searches. To perform deep traversal searches, you must create search folders.
You can also search for items by using an XML message. For more information, see FindItem Operation.
The SOAP messages that are passed between the Exchange Web Services client and server are defined by the XML schema and WSDL files. The XML schema and WSDL files define the contract between the client and server. Proxy class generators create an object-model abstraction of those SOAP messages, which can simplify programming. This code example uses a proxy class library that was generated by Microsoft Visual Studio 2005. Different proxy class generators create different object models for a given Web service. This proxy class code example is an illustration only. Refer to the proxy class generator documentation for support for proxy classes.
Compiling the Code
For information about compiling the code, see Exchange Web Services Client Development.
Robust Programming
The namespace for the proxy assembly is defined when the proxy assembly is created.