Search code examples
sharepointmicrosoft-graph-apisharepoint-online

Can I use Graph Api to filter files in a SharePoint Folder by createdDateTime?


I'm trying to filter the files within a Sharepoint folder to find those which have been uploaded in the last 24 hours.

I've found that https://graph.microsoft.com/v1.0/drives/[driveID]/items/[folderID]/children
does return the files I want to process, but when I try to filter the result with
$filter=createdDateTime ge [Date]
I get an "Invalid request" error.

What is weird is that I get an "Incomaptible type" error when I accidentally passed the date as a string. Meaning the filter does recognize the type of the field...?

I've been trying to get all the files via
https://graph.microsoft.com/v1.0/drives/[driveID]/items
and then filtering them using the FolderID. like this: https://graph.microsoft.com/v1.0/drives/[DriveID]/items?$expand=analytics($expand=allTime)&$filter=parentReference/id eq '[FolderID]' and createdDateTime ge [Date]
But this gives a "General exception while processing" error with no further details...

Does anyone know if it's possible to filter the files in a Sharepoint folder by date created?


Solution

  • Filtering driveItem children is limited only to item name. The Graph API calls underlaying OneDrive API and the OneDrive API itself doesn't support filtering by createddatetime.

    I'm using \search\query endpoint. In the body, you can specify the filter and reduce the searching to specific SharePoint folder

    {
        "requests": [
            {
                "entityTypes": [
                    "driveItem"
                ],
                "query": {
                    "queryString": "created>=2023-07-25 AND path:\"https://<tenant>.sharepoint.com/sites/<site_name>/Shared%20documents\""
                }
            }
        ]
    }
    

    Return only documents:

    {
        "requests": [
            {
                "entityTypes": [
                    "driveItem"
                ],
                "query": {
                    "queryString": "created>=2023-07-25 AND path:\"https://<tenant>.sharepoint.com/sites/<site_name>/Shared%20documents\" AND isDocument=true"
                }
            }
        ]
    }