Filtering on IsGreaterThanOrEqualTo by using the EWS Managed API 2.0
Last modified: October 13, 2012
Applies to: EWS Managed API | Exchange Server 2007 Service Pack 1 (SP1) | Exchange Server 2010
Note: This content applies to the EWS Managed API 2.0 and earlier versions. For the latest information about the EWS Managed API, see Web services in Exchange.
You can use the Microsoft Exchange Web Services (EWS) Managed API to create an SearchFilter.IsGreaterThanOrEqualTo search filter. The IsGreaterThanOrEqualTo filter enables you to search an Exchange mailbox for items or folders that contain an extended property that is greater than or equal to a compare value.
To use an IsGreaterThanOrEqualTo search filter
Define a view along with a page size. The following code defines an ItemView with a page size of 10.
ItemView view = new ItemView(10);
The page size is required to create an ItemView. Other optional parameters for the ItemView constructor are the Offset and the OffsetBasePoint. The Offset and OffsetBasePoint are used for subsequent paged calls.
Optionally, to enhance performance, identify the properties to return in the results set. The following example shows a property set that includes the item Id and Subject properties.
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject);
Create an extended property that contains an integer. This is the property that will be searched for a compare value.
Guid MyPropertySetIdint = new Guid("{75A5486F-9267-49ca-9B4E-3D04CA9EC179}"); ExtendedPropertyDefinition extendedPropertyDefinitionint = new ExtendedPropertyDefinition(MyPropertySetIdint, "MyFlag", MapiPropertyType.Integer);
Create the search filter. The following example shows a search filter that is used to find items that contain an extended property MyFlag the value for which is greater than or equal to six.
SearchFilter IsGreaterThanOrEqualsearchFilter = new SearchFilter.IsGreaterThanOrEqualTo(extendedPropertyDefinitionint, 6);
Send the request to search the mailbox and get the results. In this example, the search is limited to the mailbox user's Inbox.
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, IsGreaterThanOrEqualsearchFilter, view);
Do something with the FindItemsResult object.
Example
The following example shows how to search the Exchange mailbox for any e-mail message in the user's Inbox that has the defined extended property and for which the value of that extended property is greater than or equal to the compare value. The example displays the total number of e-mail messages that satisfy the search filter.
// Obtain a collection of e-mail messages that satisfy a specified search filter.
ItemView view = new ItemView(10);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject);
// Create the extended property MyFlag.
Guid MyPropertySetIdint = new Guid("{75A5486F-9267-49ca-9B4E-3D04CA9EC179}");
ExtendedPropertyDefinition extendedPropertyDefinitionint = new ExtendedPropertyDefinition(MyPropertySetIdint, "MyFlag", MapiPropertyType.Integer);
SearchFilter IsGreaterThanOrEqualsearchFilter = new SearchFilter.IsGreaterThanOrEqualTo(extendedPropertyDefinitionint, 6);
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, IsGreaterThanOrEqualsearchFilter, view);
Console.WriteLine("IsGreaterThanOrEqualTo: Total e-mail count with an extended property value is greater than or equal to 6: " + findResults.TotalCount);
This example assumes that the ExchangeService object named service is correctly configured for connecting to the user's Client Access server.
The following example shows the XML request that is sent by the client to the server when an IsGreaterThanOrEqualTo search filter is used to search an Exchange mailbox.
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types"
xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<t:RequestServerVersion Version="Exchange2010" />
</soap:Header>
<soap:Body>
<m:FindItem Traversal="Shallow">
<m:ItemShape>
<t:BaseShape>IdOnly</t:BaseShape>
<t:AdditionalProperties>
<t:FieldURI FieldURI="item:Subject" />
</t:AdditionalProperties>
</m:ItemShape>
<m:IndexedPageItemView MaxEntriesReturned="10" Offset="0" BasePoint="Beginning" />
<m:Restriction>
<t:IsGreaterThanOrEqualTo>
<t:ExtendedFieldURI PropertySetId="75a5486f-9267-49ca-9b4e-3d04ca9ec179"
PropertyName="MyFlag" PropertyType="Integer" />
<t:FieldURIOrConstant>
<t:Constant Value="6" />
</t:FieldURIOrConstant>
</t:IsGreaterThanOrEqualTo>
</m:Restriction>
<m:ParentFolderIds>
<t:DistinguishedFolderId Id="inbox" />
</m:ParentFolderIds>
</m:FindItem>
</soap:Body>
</soap:Envelope>
The following example shows the XML response that is returned by the server after it parses the request from the client.
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="14" MinorVersion="0" MajorBuildNumber="499"
MinorBuildNumber="0" Version="Exchange2010"
xmlns:h="https://schemas.microsoft.com/exchange/services/2006/types"
xmlns="https://schemas.microsoft.com/exchange/services/2006/types"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<m:FindItemResponse xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
<m:ResponseMessages>
<m:FindItemResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
<m:RootFolder IndexedPagingOffset="5" TotalItemsInView="5" IncludesLastItemInRange="true">
<t:Items>
<t:Message>
<t:ItemId Id="AAMkADBkODhkYzc0LTcyYmQtNDlhOS04NTRhLTY0NzdjYTljNWY3NQBGAAAAAADXXYVKp
76rSYJm9c5Ib1v+BwBG1Sh8748rS6HAt4gVZYTuAAAAAAAPAABG1Sh8748rS6HAt4gVZYTuAAAKg/luAAA="
ChangeKey="CQAAABYAAABG1Sh8748rS6HAt4gVZYTuAAAKhDyk" />
<t:Subject>Saved with extendedPropertyDefinition (MyFlag) value of 8</t:Subject>
</t:Message>
<t:Message>
<t:ItemId Id="AAMkADBkODhkYzc0LTcyYmQtNDlhOS04NTRhLTY0NzdjYTljNWY3NQBGAAAAAADXXYVKp
76rSYJm9c5Ib1v+BwBG1Sh8748rS6HAt4gVZYTuAAAAAAAPAABG1Sh8748rS6HAt4gVZYTuAAAKg/ltAAA="
ChangeKey="CQAAABYAAABG1Sh8748rS6HAt4gVZYTuAAAKhDyW" />
<t:Subject>Saved with extendedPropertyDefinition (MyFlag) value of 8</t:Subject>
</t:Message>
<t:Message>
<t:ItemId Id="AAMkADBkODhkYzc0LTcyYmQtNDlhOS04NTRhLTY0NzdjYTljNWY3NQBGAAAAAADXXYVKp
76rSYJm9c5Ib1v+BwBG1Sh8748rS6HAt4gVZYTuAAAAAAAPAABG1Sh8748rS6HAt4gVZYTuAAAKg/lpAAA="
ChangeKey="CQAAABYAAABG1Sh8748rS6HAt4gVZYTuAAAKhDxd" />
<t:Subject>Saved with extendedPropertyDefinition (MyFlag) value of 6</t:Subject>
</t:Message>
<t:Message>
<t:ItemId Id="AAMkADBkODhkYzc0LTcyYmQtNDlhOS04NTRhLTY0NzdjYTljNWY3NQBGAAAAAADXXYVKp
76rSYJm9c5Ib1v+BwBG1Sh8748rS6HAt4gVZYTuAAAAAAAPAABG1Sh8748rS6HAt4gVZYTuAAAKg/lhAAA="
ChangeKey="CQAAABYAAABG1Sh8748rS6HAt4gVZYTuAAAKhDOm" />
<t:Subject>Saved with extendedPropertyDefinition (MyFlag) value of 6</t:Subject>
</t:Message>
<t:Message>
<t:ItemId Id="AAMkADBkODhkYzc0LTcyYmQtNDlhOS04NTRhLTY0NzdjYTljNWY3NQBGAAAAAADXXYVKp
76rSYJm9c5Ib1v+BwBG1Sh8748rS6HAt4gVZYTuAAAAAAAPAABG1Sh8748rS6HAt4gVZYTuAAAKg/lgAAA="
ChangeKey="CQAAABYAAABG1Sh8748rS6HAt4gVZYTuAAAKhDOY" />
<t:Subject>Saved with extendedPropertyDefinition (MyFlag) value of 6</t:Subject>
</t:Message>
</t:Items>
</m:RootFolder>
</m:FindItemResponseMessage>
</m:ResponseMessages>
</m:FindItemResponse>
</s:Body>
</s:Envelope>
Compiling the code
For information about compiling this code, see Getting started with the EWS Managed API 2.0.
Robust programming
Write appropriate error handling code for common search errors.
Review the client request XML that is sent to the Exchange server.
Review the server response XML that is sent from the Exchange server.
Set the service binding as shown in Setting the Exchange service URL by using the EWS Managed API 2.0. Do not hard code URLs because if mailboxes move, they might be serviced by a different Client Access server. If the client cannot connect to the service, retry setting the binding by using the AutodiscoverUrl(String) method.
Set the target Exchange Web Services schema version by setting the requestedServerVersion parameter of the ExchangeService constructor. For more information, see Versioning EWS requests by using the EWS Managed API 2.0.
Security
Use HTTP with SSL for all communication between client and server.
Always validate the server certificate that is used for establishing the SSL connections. For more information, see Validating X509 certificates by using the EWS Managed API 2.0.
Do not include user names and passwords in trace files.
Verify that Autodiscover lookups that use HTTP GET to find an endpoint always prompt for user confirmation; otherwise, they should be blocked.