This post was most recently updated on March 31st, 2023.
2 min read.This article explains one possible fix to an annoyingly generic exception thrown by the Microsoft Graph API (namely, “Unsupported or invalid query filter clause specified for property”) or an even more generic exception thrown by PnP PowerShell (“Exception of type ‘PnP.PowerShell.Commands.Model.Graph.GraphException’ was thrown.”) when you’re trying to query Graph API with what’s called “Advanced query capabilities” (operators like less than or equal, greater than or equal) instead of just checking if something is equal. I ran into this with DateTimes, but I’m sure you can run into this with many different data types.
But as usual, let’s take a step back. What were the errors, and what was the reason?
Problem
There are quite a few different errors that you can run into due to this same issue – I’m logging a few of them down here.
If you’re running PnP PowerShell:
"Exception of type 'PnP.PowerShell.Commands.Model.Graph.GraphException' was thrown."
And if you dig in, the category of the error is InvalidOperation, but that’s about all you can get.
With Graph Explorer, you might get this:
{ "error": { "code": "Request_UnsupportedQuery", "message": "Unsupported or invalid query filter clause specified for property 'expirationDateTime' of resource 'Group'.", "innerError": { "date": "2022-10-14T10:28:16", "request-id": "0f0abe32-3da9-4f0e-b59d-72f65ed66b93", "client-request-id": "25cc8dc7-5d3a-a543-34a1-dd21346573f8" } } }
And if you’re using the Graph SDK, God knows what you’ll get and if there’s any way to catch that Exception. Probably not.
Reason
There are plenty of Microsoft Graph API queries that require some extra magic to be set before your query will be successfully executed. Shamefully, neither the API nor the Graph Explorer are able to help you much – but the right answers are luckily hidden in the documentation if you’re interested in googling hard enough :)
See, the documentation has this note:
Important!
Specific usage of
A detail hidden deep in the documentation.$filter
and the$search
query parameter is supported only when you use the ConsistencyLevel header set toeventual
and$count
. For more information, see Advanced query capabilities on Azure AD directory objects.
And then – how did you fix this, again?
Solution
Okay – let’s go through, step by step, what to do.
Time needed: 5 minutes
How to make Graph API respond properly to your Advanced queries?
- (If you’re using Graph Explorer): Add a new header in Graph Explorer
In case your issue is with Graph Explorer, navigate to the “Request headers” tab, and add a new header with the following information:
Key: “ConsistencyLevel”
Value: “eventual” - (If you’re getting this error in your code): Add a new header to your request
Okay, you’ll need something like this:
var additionalHeaders = new Dictionary();
additionalHeaders.Add("ConsistencyLevel", "eventual");
var message = new HttpRequestMessage();
foreach (var kv in additionalHeaders)
{
message.Headers.Remove(kv.Key);
message.Headers.Add(kv.Key, kv.Value);
}
// ... and whatever else you wanted to have in your message
httpClient.SendAsync(message); - Add “$count=true” to your query
Now your query URL also needs to have the $count parameter – and it needs to have a value “true”.
So in your actual request (doesn’t matter whether it’s in Graph Explorer or your code), it’ll look something like this:
https://graph.microsoft.com/v1.0/groups?$filter=groupTypes/any(c:c+eq+’Unified’) and expirationDateTime le 2022-12-13T19:52:17Z&$count=true
Emphasis on the “$count=true” at the end of the query.
That’s it. That’s the article. That was the trick. Really.
Let me know if it helps you :)
References
- Documentation that hints at what you’re missing
- Documentation of why you’re required to have this arcane magic in your requests
- How to stop Surface Headphones from autoplaying audio when you place them on the desk? - December 10, 2024
- “Phone Link” permanently disabled and Windows claiming “Some of these settings are managed by your organization”? (Probably) an easy fix! - December 3, 2024
- Refreshing DefaultAzureCredential in Visual Studio - November 26, 2024