none
Search for all items in all folders where a specific mail address is involved (EWS) RRS feed

  • Question

  • Hi everybody

    I'm trying to search for all mail items in all folders where a specific mail address (e.g. dummy@domain.com) is involved (from, to, cc, bcc). How to achieve that with EWS?

    I've tried to use two filters. One for incoming messages, one for outgoing messages:

    PropertyDefinitionBase transportMessageHeaders = new ExtendedPropertyDefinition(0x007D, MapiPropertyType.String);
    
                var incomingFilters = new SearchFilter.SearchFilterCollection(LogicalOperator.And)
                {
                    new SearchFilter.IsEqualTo(EmailMessageSchema.From, mailAddress),
                    new SearchFilter.ContainsSubstring(ItemSchema.ItemClass, ItemClassKeys.MailMessage),
                    new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived,
                        DateTime.Now.AddDays(-30))
                };
                var outgoingFilters = new SearchFilter.SearchFilterCollection(LogicalOperator.And)
                {
                    new SearchFilter.ContainsSubstring(transportMessageHeaders, mailAddress, ContainmentMode.Substring,
                        ComparisonMode.IgnoreCaseAndNonSpacingCharacters),
                    new SearchFilter.ContainsSubstring(ItemSchema.ItemClass, ItemClassKeys.MailMessage),
                    new SearchFilter.IsNotEqualTo(EmailMessageSchema.From, mailAddress),
                    new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived,
                            DateTime.Now.AddDays(-30))
                };


    To get the items from all folders I'm using the 'AllItems' folder:

    private FolderId GetAllItemsFolderId(ExchangeService ews)
            {
                var searchFilterCollection = new SearchFilter.SearchFilterCollection(LogicalOperator.And)
                {
                    new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "AllItems"),
                    new SearchFilter.IsEqualTo(FolderSchema.FolderClass, "IPF")
                };
                
                var view = new FolderView(1) { Traversal = FolderTraversal.Shallow };
                FindFoldersResults folderResults = ews.FindFolders(WellKnownFolderName.Root, searchFilterCollection, view);
                return folderResults.First().Id;
            }

    My current solution has two problems:

    • It doesn't find sent items to the specific mail address
    • It doesn't find items in the Conversation History folder

    Any ideas?

    Wednesday, October 12, 2016 2:34 PM

All replies

  • The transportheader won't work for those two types of items because that property won't exist on them. My suggestion would be you use AQS and then search using the participants property at least for those two folder eg

    service.FindItems(FolderId, "participants:mail@domain.com", view);

    you can't use that method to track BCC's either it won't really work by design, depending on what you trying to do(eg if your doing it for admin or audit purposes) then Message Tracking is a better option as it will show all the details of how a message was routed and who it was delivered to .

    Cheers
    Glen

    Thursday, October 13, 2016 1:48 AM
  • Hi guys

    Thank you for your answers. They have pointed me to the right direction. My current solution works pretty well so far:

    private IEnumerable<EmailMessage> GetMessages(ExchangeService ews, string query)
    {
         ...
         foreach (Folder folder in GetMessageFolders(ews))
         {
              FindItemsResults<Item> results = ews.FindItems(folder.Id, "participants:mail@domain.com", view);
              ...
              return messages;
         }
    
    private IEnumerable<Folder> GetMessageFolders(ExchangeService ews)
    {
         var folderSearch = new SearchFilter.SearchFilterCollection(LogicalOperator.And)
         {
              new    SearchFilter.ContainsSubstring(FolderSchema.FolderClass, "IPF.Note")
         };
    
         var folders = ews.FindFolders(WellKnownFolderName.MsgFolderRoot, folderSearch, new FolderView(50)
         {
              Traversal = FolderTraversal.Deep,
              PropertySet = new PropertySet(BasePropertySet.IdOnly) { FolderSchema.WellKnownFolderName }
         });
    
         return folders.Where(x => x.WellKnownFolderName != WellKnownFolderName.Conflicts
         && x.WellKnownFolderName != WellKnownFolderName.LocalFailures
         && x.WellKnownFolderName != WellKnownFolderName.ServerFailures
         && x.WellKnownFolderName != WellKnownFolderName.SyncIssues);
    }

    Tuesday, October 18, 2016 10:43 AM