Member-only story
Handling Filtering Issues with Microsoft Graph API: Filtering on companyName and Other User Properties
When using Microsoft Graph API to filter user data, you may face issues where certain properties do not return the expected results. For example, if you’re trying to filter on companyName
, the query may not work unless you explicitly enable the $count
parameter and set the ConsistencyLevel
header. This blog post explains the reason behind this issue and provides the correct way to filter on such properties.
Problem:
Let’s say you want to retrieve users filtered by their companyName
. A query like the one below may seem correct but doesn’t return the expected results:
https://graph.microsoft.com/v1.0/users?$select=companyName&$filter=companyName eq 'Test Company'
In most cases, you won’t get the filtered users because the companyName
property (and several other properties) require additional query options and headers.
Solution:
To resolve this, you need to use the following approach:
- Use the
$count=true
parameter: Adding$count=true
enables you to get a count of the filtered users, but more importantly, it triggers the proper filtering mechanism for certain properties. - Add the
ConsistencyLevel: eventual
header: This header ensures that the query operates under eventual consistency, which is necessary for filtering certain properties that are not indexed…