Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading