Unable to filter emails by multiple categories with Graph

Rafa 0 Reputation points
2024-05-07T06:47:39.59+00:00

Hello,

I am trying to filter emails by multiple categories with Microsoft Graph, but I'm not able to get it to work. I found this thread which gives me a query to filter by category 1 OR category 2:

https://learn.microsoft.com/en-us/answers/questions/666330/microsoft-graph-api-filtering-on-categories


$filter=categories/any(a:a eq 'Orange Category') OR categories/any(b:b eq 'Red Category')

I also found this thread which has the exact same problem as I do.

https://learn.microsoft.com/en-us/answers/questions/1418833/microsoft-graph-filter-messages-by-categories?source=docs

Here's the query they use:


$filter=categories/any(a:a eq 'Orange Category') AND categories/any(b:b eq 'Red Category')

Just as in that thread, I tried a similar query, and it just yields an empty array. It does not seem to work, and the link to a related GitHub issue is dead.

Here's the query I used in Graph Explorer that yields an empty array:


https://graph.microsoft.com/v1.0/me/messages?$filter=categories/any(a:a+eq+'__One')+AND+categories/any(b:b+eq+'_Two')

My mailbox has mails with category __One, mail with category _Two, mail with both categories, and mail with none or only the category "_Three".

What is wrong in my query and how can I filter emails by multiple categories with Microsoft Graph?

Thanks in advance for the help.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,787 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Carolyne-3676 201 Reputation points
    2024-05-09T17:21:15.4533333+00:00

    Thanks for raising this issue.

    Typically using the AND operator, as in your query, will return messages that have both '__One' and '_Two' categories. It will not return messages that have only one of these categories. When using the AND operator, it requires that both conditions be met, meaning it will only return emails that have both '__One' and '_Two' as categories.

    Have you tried the OR operator. This should allow you to retrieve messages that have either '__One' or '_Two' categories. Ensure that the category names are correct and that there are emails in your mailbox with these categories. I hope this helps.