static void FindItem()
{
// Create the service binding.
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Credentials = new NetworkCredential("username", "password", "domain");
esb.Url = @"https://ExchangeServer/EWS/Exchange.asmx";
// Form the FindItem request.
FindItemType findItemRequest = new FindItemType();
// Define the paging scheme for the result set.
IndexedPageViewType ipvt = new IndexedPageViewType();
ipvt.BasePoint = IndexBasePointType.Beginning;
ipvt.MaxEntriesReturned = 10;
ipvt.MaxEntriesReturnedSpecified = true;
ipvt.Offset = 0;
// Add the paging scheme to the request.
findItemRequest.Item = ipvt;
// Define the grouping scheme for the result set.
GroupByType group = new GroupByType();
// Define the property that is used to determine the order of groups of items in a group.
AggregateOnType aggregate = new AggregateOnType();
PathToUnindexedFieldType subject = new PathToUnindexedFieldType();
subject.FieldURI = UnindexedFieldURIType.itemSubject;
aggregate.Item = subject;
aggregate.Aggregate = AggregateType.Minimum;
group.AggregateOn = aggregate;
// Define the property that is used to group items.
PathToUnindexedFieldType importance = new PathToUnindexedFieldType();
importance.FieldURI = UnindexedFieldURIType.itemImportance;
group.Item = importance;
// Define how the groups are ordered in the response.
group.Order = SortDirectionType.Descending;
// Add the grouping scheme to the request.
findItemRequest.Item1 = group;
// Define the item properties that are returned in the response.
ItemResponseShapeType itemProperties = new ItemResponseShapeType();
// Use the Default shape for the response.
itemProperties.BaseShape = DefaultShapeNamesType.Default;
// Add more properties to the request.
PathToUnindexedFieldType addIsMeeting = new PathToUnindexedFieldType();
PathToUnindexedFieldType addImportance = new PathToUnindexedFieldType();
addIsMeeting.FieldURI = UnindexedFieldURIType.calendarIsMeeting;
addImportance.FieldURI = UnindexedFieldURIType.itemImportance;
itemProperties.AdditionalProperties = new PathToUnindexedFieldType[2];
itemProperties.AdditionalProperties[0] = addIsMeeting;
itemProperties.AdditionalProperties[1] = addImportance;
// Add the properties shape to the request.
findItemRequest.ItemShape = itemProperties;
// Identify which folders to search.
DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
folderIDArray[0] = new DistinguishedFolderIdType();
folderIDArray[0].Id = DistinguishedFolderIdNameType.calendar;
// Add folders to the request.
findItemRequest.ParentFolderIds = folderIDArray;
// Create a restriction for the result set.
RestrictionType restriction = new RestrictionType();
PathToUnindexedFieldType pteft = new PathToUnindexedFieldType();
pteft.FieldURI = UnindexedFieldURIType.calendarStart;
FieldURIOrConstantType fieldURIORConstant = new FieldURIOrConstantType();
fieldURIORConstant.Item = new ConstantValueType();
(fieldURIORConstant.Item as ConstantValueType).Value = DateTime.Now.ToString();
IsGreaterThanType isGreaterThan = new IsGreaterThanType();
isGreaterThan.Item = pteft;
isGreaterThan.FieldURIOrConstant = fieldURIORConstant;
restriction.Item = isGreaterThan;
findItemRequest.Restriction = restriction;
// Define the sort order of items.
FieldOrderType[] fieldsOrder = new FieldOrderType[1];
fieldsOrder[0] = new FieldOrderType();
PathToUnindexedFieldType subjectOrder = new PathToUnindexedFieldType();
subjectOrder.FieldURI = UnindexedFieldURIType.itemSubject;
fieldsOrder[0].Item = subjectOrder;
fieldsOrder[0].Order = SortDirectionType.Ascending;
findItemRequest.SortOrder = fieldsOrder;
// Define the traversal type.
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
try
{
// Send the FindItem request and get the response.
FindItemResponseType findItemResponse = esb.FindItem(findItemRequest);
// Access the response message.
ArrayOfResponseMessagesType responseMessages = findItemResponse.ResponseMessages;
ResponseMessageType responseMessage = responseMessages.Items[0];
if (responseMessage is FindItemResponseMessageType)
{
FindItemResponseMessageType firmt = (responseMessage as FindItemResponseMessageType);
FindItemParentType fipt = firmt.RootFolder;
object obj = fipt.Item;
// Determine whether the FindItem response contains grouped items.
if (obj is ArrayOfGroupedItemsType)
{
ArrayOfGroupedItemsType groupedItems = (obj as ArrayOfGroupedItemsType);
//TODO: Write code to handle grouped items.
}
// FindItem contains an array of items.
else if (obj is ArrayOfRealItemsType)
{
ArrayOfRealItemsType items = (obj as ArrayOfRealItemsType);
//TODO: Write code to handle items.
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}