Thomas
Thomas

Reputation: 21

EWS SearchFilter is not working with some email adresses when using FindItems on a folder

The following code below returns zero items.

EWSService = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
            EWSService.TraceListener = tr;
            EWSService.TraceFlags = TraceFlags.DebugMessage | TraceFlags.EwsRequest | TraceFlags.EwsResponse;
            EWSService.TraceEnabled = true;
            EWSService.Credentials = new WebCredentials(user, psw,domain);
            EWSService.Url = new Uri("https://----/EWS/Exchange.asmx");

FolderId id = Test(EWSService, "inbox", null);

Folder source = Microsoft.Exchange.WebServices.Data.Folder.Bind(EWSService, id);

SearchFilter> slist = new List<SearchFilter> ();

Add(new SearchFilter.IsEqualTo(EmailMessageSchema.From, "[email protected]"));

SearchFilter filter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, slist);

ItemView messageView = new ItemView(99);
FindItemsResults<Item> list = source.FindItems(filter,messageView);

the list sometimes contains 0 items when I use a specific email address in the searchFilter even when the mail item is present in the folder.

When I don't use a SearchFilter with FindItems it is present in the list.

How come the SearchFilter is not working ?

Upvotes: 2

Views: 1749

Answers (1)

rojobo
rojobo

Reputation: 486

First off.

You DONT need a List of Searchfilter, if you only want to look for ONE email address

SearchFilter> slist = new List<SearchFilter> ();

Now on to some recommendations:

  1. I'd recommend using a query string instead of a SearchFilter.

     // Find all items where the From: contains "[email protected]".
      string filter= "From:\"[email protected]\"";
    

    Source: https://msdn.microsoft.com/en-us/library/office/dn579420(v=exchg.150).aspx

  2. Do not pull 99 items in the ItemView instead pull 20 and use pagination

    ItemView messageView = new ItemView(20, 0, OffsetBasePoint.Beginning);
    
  3. Load only the properties that you NEED

    messageView.PropertySet = BasePropertySet.IdOnly;
    
  4. Define how deep do you want to search

    messageView.Traversal = ItemTraversal.Shallow
    

The code below is ONLY an example of how I've used the findItems method in the past for my own projects using VB... FOR DEMONSTRATION PURPOSES

Private Function GetAllSyncedContactIdsInExchange(pService As ExchangeService) As List(Of Integer)
    Dim oInternalContactIdDefinition As New ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, conContactIdPropertyName, MapiPropertyType.Integer)
    Dim oInternalContactIdFilter As New SearchFilter.Exists(oInternalContactIdDefinition)
    Dim oResults As FindItemsResults(Of Item) = Nothing
    Dim oPropertySet As New PropertySet(oInternalContactIdDefinition)
    Dim lstSyncedContactIds As New List(Of Integer)
    Dim intDBId As Integer
    Dim lstEESContactFolders As List(Of FolderId) = GetAllCustomEESFolderIds(pService)

    For Each oFolderId As FolderId In lstEESContactFolders
        Dim blnMoreAvailable As Boolean = True
        Dim intSearchOffset As Integer = 0
        Dim oView As New ItemView(conMaxChangesReturned, intSearchOffset, OffsetBasePoint.Beginning)
        oView.PropertySet = BasePropertySet.IdOnly

        Do While blnMoreAvailable
            oResults = pService.FindItems(oFolderId, oInternalContactIdFilter, oView)
            blnMoreAvailable = oResults.MoreAvailable
            If Not IsNothing(oResults) AndAlso oResults.Items.Count > 0 Then
                pService.LoadPropertiesForItems(oResults, oPropertySet)
                For Each oExchangeItem As Item In oResults.Items
                    If oExchangeItem.TryGetProperty(oInternalContactIdDefinition, intDB2Id) Then
                        lstSyncedContactIds.Add(intDBId)
                    End If
                Next
                If blnMoreAvailable Then oView.Offset = oView.Offset + conMaxChangesReturned
            End If
        Loop
    Next

    Return lstSyncedContactIds
End Function

Upvotes: 1

Related Questions