Home > Mobile >  Microsoft Graph Rest API - Filter by last X days
Microsoft Graph Rest API - Filter by last X days

Time:09-14

I have a rest URL that I am running against Microsoft Graph via POSTMAN and I'd like to know how to make the date filter check for records with activity in the last 90 days. My URL is this:

https://graph.microsoft.com/beta/users?$select=signInActivity&$filter=signInActivity/lastSignInDateTime le 2022-09-01

I do not want to use the hard-coded date like in this example or have to lean on something like powershell. Is there a way to calculate the date in the url or to tell the filter to do so? Something like this would be nice:

https://graph.microsoft.com/beta/users?$select=signInActivity&$filter=signInActivity/lastSignInDateTime le (today()-days(90))

CodePudding user response:

Graph API does not support functions for working with date.

You can declare a property in Postman and set the property before the request is sent.

Example

The Graph API request is in a Postman collection. You can create a collection variable MyVar and use the variable in $filter query.

enter image description here

Then you can write a pre-request script in JavaScript and set the collection variable.

let d = new Date();
d.setDate(d.getDate()-90);
pm.collectionVariables.set("MyVar", d.toISOString().split('T')[0]);

enter image description here

Send the request and check console how the request looks like.

enter image description here

Resources:

Filter query operators

  • Related