microsoft-graph-apimicrosoft-graph-files

Getting error "invalidRequest" when filtering based on lastModifiedDateTime (Microsoft Graph API)


I am using the below filters for the graph api request.

?$filter=lastModifiedDateTime ge 2023-10-06T06:48:54Z&$select=name,lastModifiedDateTime,@microsoft.graph.downloadUrl

I am getting the following error:

{
    "error": {
        "code": "invalidRequest",
        "message": "Invalid request"
    }
}

The request cannot be fulfilled due to bad syntax.

This is the list of items without the filter condition.

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(driveItem)",
    "value": [
        {
            "@odata*.etag": "***",
            "lastModifiedDateTime": "2023-10-06T06:48:54Z",
            "name": "Archive"
        },
        {
            "@odata.etag": "**",
            "lastModifiedDateTime": "2024-05-07T06:53:10Z",
            "name": "Pricing_Reference_Data_from_Source_Report.xlsx"
        }
    ]
}

Could you please help me understand what the issue with the above syntax is? Thanks in advance.


Solution

  • For your query to work, you need to specify the Prefer request header with the value TryFilterLastModifiedDateTimeTimeWarningMayFailRandomly,apiversion=2.1.

    GET /v1.0/sites/{site_id}/drives/{drive_id}/root:/{folder_name}:/children?$filter=lastModifiedDateTime ge datetime '2023-10-06T06:48:54Z'&$select=name,lastModifiedDateTime,@microsoft.graph.downloadUrl
    
    Prefer: TryFilterLastModifiedDateTimeTimeWarningMayFailRandomly,apiversion=2.1
    

    The apiversion=2.1 will ensure that your request is routed to internal beta SharePoint API . Because it's internal beta API, it's not recommended for the production.

    The TryFilterLastModifiedDateTimeTimeWarningMayFailRandomly will ensure that you can filter drive items by lastModifiedDateTime, but as the name says, the filtering can fail randomly.