Home > Enterprise >  Invalid filter clause when trying to retrieve last sign in date time from Graph
Invalid filter clause when trying to retrieve last sign in date time from Graph

Time:11-07

string date = DateTime.Today.AddDays(-30).ToString("yyyy-MM-ddTHH-mm-ssZ");

var users = await client.Users
                    .Request()
                    .Filter($"signInActivity/lastSignInDateTime le {date}")
                    .GetAsync();

This comes back with invalid filter clause error but seems to match that of the filter detailed here:

https://docs.microsoft.com/en-us/graph/api/user-list?view=graph-rest-beta&tabs=http#example-5-list-the-last-sign-in-time-of-users-in-a-specific-time-range

I've inspected the message with seems to match up too. What have I missed?

CodePudding user response:

I have tried these two requests in Microsoft Graph Explorer:

https://graph.microsoft.com/beta/users?$filter=signInActivity/lastSignInDateTime le 2021-07-21T00-00-00Z

which returns an Invalid filter clause. This request uses your formatting for the date.

https://graph.microsoft.com/beta/users?$filter=signInActivity/lastSignInDateTime le 2021-07-21T00:00:00Z

which returns Calling principal does not have required MSGraph permissions AuditLog.Read.All but it is normal since I was not authenticated and used the sample instance from Microsoft. I think you won't have this error on your instance. The idea is that the filter seems to be correct here.

The issue seems to be in your formatting, you should try ToString("yyyy-MM-ddTHH:mm:ssZ") instead of ToString("yyyy-MM-ddTHH-mm-ssZ").

  • Related