microsoft-graph-apimicrosoft-graph-sdksmicrosoft-graph-mail

MS Graph API: filter messages by subject length


Is there a way to filter (or search) for outlook email messages where the subject length is less than (say) 20 characters using the Microsoft Graph API?

I've tried with a filter:

https://graph.microsoft.com/v1.0/me/messages?$filter=strlen(subject) le 20

but the strlen function is not recognised (also tried subject.length and subject.size but those are not recognised either).

No luck with search either:

https://graph.microsoft.com/v1.0/me/messages?$search="strlen(subject) le 20"

which just returns no results.


Solution

  • Odata supports this using length http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#sec_length eg

    $filter=length(Subject) eq 19
    

    But Microsoft haven't implemented this in the Graph the only valid operators are listed https://learn.microsoft.com/en-us/graph/filter-query-parameter?tabs=http so startsWith,endswidth and contains

    In any of the Exchange API's there doesn't exist a restriction to allow you to do that at the server level. The closest is in KQL the near operator https://learn.microsoft.com/en-us/sharepoint/dev/general-development/keyword-query-language-kql-syntax-reference which you can do some pretty dynamic things with when matching around a subject. Otherwise the only option would be to enumerate and filter once you get the result.