Search code examples
outlookmicrosoft-graph-apimicrosoft-exchange

Filter Microsoft Graph List Messages API to only received messages


Goal: I'd like to be able to filter the "List Messages" API to only return emails that are sent to the user rather than any messages that exist anywhere (such as ones sent out by the user).

Right now, if you query this particular API, you get a list of all of the messages in all folders (including Deleted Items and Clutter folders).

I know that I can filter on isDraft eq false to remove the drafts - but I don't know if there's any filter to say don't include messages sent by the user.

What I've Tried: I've looked over the examples and didn't see anything about filtering like this. I've also taken a look at the OData Query Params and dug into the filter param. The problem is I'm not quite sure what I could even filter on. I do see that there is a sender parameter - but given I've connected via OAuth and haven't asked users to provide their email address I'm not sure I have an easy way to filter on that.

Theoretically, I could first do a request to the https://graph.microsoft.com/beta/me/ endpoint to get the userPrincipalName and then add a filter on from/emailAddress/address ne '<userPrincipalName>' when I call the /me/messages endpoint - but that's going to double the number of API calls I'd need to make. Is that the only option I have or is there a better way to go about this?

Thanks for your time :)


Solution

  • There is no filter parameter that will achieve this directly. If you want to filter away sent messages - you'll need to either decode the base64 encoded JWT access token and extract the userPrincipalName or make a call to the /me endpoint and retrieve the userPrincipalName from that. Once you have that, you can add a filter of from/emailAddress/address ne '<userPrincipalName>'.

    Please note that if you are filtering on many things and you have an "order by" param that you'll need to include that param in your filter or you'll receive an error about too many filters.