Search code examples
.net-coremicrosoft-graph-apimicrosoft-graph-sdks

Using filter with Microsoft Graph gives an OData error


When I use Microsoft Graph library for .NET, I get an error whenever I try to filter on a mail, when listing users in our organization. I am using .NET 6.0 and the newest version of Microsoft.Graph

Below is my function. When I run this, it gives me this error. If I comment the .Filter line out, it suddenly works. Any reason why the .Filter does not work?

var users = _appClient.Users.GetAsync((config) =>
                {
                    config.QueryParameters.Filter = "endswith(mail,'@org.com')";
                    // Only request specific properties
                    config.QueryParameters.Select = new[]
                        { "displayName", "id", "mail", "jobTitle", "officeLocation", "userPrincipalName" };
                    // Get at most 25 results
                    config.QueryParameters.Top = 25;
                    // Sort by display name
                    config.QueryParameters.Orderby = new[] { "displayName" };
                });

                return users;

Error message: An unhandled exception has occurred while executing the request. Microsoft.Graph.Models.ODataErrors.ODataError: Exception of type 'Microsoft.Graph.Models.ODataErrors.ODataError' was thrown.


Solution

  • Try to add ConsistencyLevel header and $count true

    var users = _appClient.Users.GetAsync((config) =>
                {
                    config.QueryParameters.Filter = "endswith(mail,'@org.com')";
                    // Only request specific properties
                    config.QueryParameters.Select = new[]
                        { "displayName", "id", "mail", "jobTitle", "officeLocation", "userPrincipalName" };
                    // Get at most 25 results
                    config.QueryParameters.Top = 25;
                    // Sort by display name
                    config.QueryParameters.Orderby = new[] { "displayName" };
                    config.Headers.Add("ConsistencyLevel", "eventual");
                    config.QueryParameters.Count = true;
                });
    
                return users;
    

    Advanced query