Search code examples
c#exchange-serverexchangewebservices

Search by category Exchange Server EWS


I need to get all the emails that have a specific category name, how would I do this?

Right now I have this:

var col = new List<SearchFilter>();
col.Add(new SearchFilter.ContainsSubstring(ItemSchema.Categories, "Processed"));
var filter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, col.ToArray());

FindItemsResults<Item> findResults = service.FindItems(
    WellKnownFolderName.Inbox,
    filter,
    new ItemView(10)
);

But that gives me a Microsoft.Exchange.WebServices.Data.ServiceResponseException that says {"The Contains filter can only be used for string properties."}

How would I do it?


Solution

  • AFAIK as of Exchange 2010, category is a multi value field so it does not work with search filters. However, you can search categories using AQS. The following code should do the trick.

    ExchangeService service = GetService(); 
    
    var iv = new ItemView(1000);
    string aqs = "System.Category:Processed";
    FindItemsResults<Item> searchResult = null;
    do
    {
        searchResult = service.FindItems(WellKnownFolderName.Inbox, aqs, iv);
        foreach (var item in searchResult.Items)
        {
            Console.WriteLine(item.Subject);
        }
        iv.Offset += searchResult.Items.Count;
    } while (searchResult.MoreAvailable == true);