How to Differentiate between folders and files

Priya Shrivastava 26 Reputation points
2022-01-07T11:20:01.847+00:00

I am making a graphi api search call on driveItem and the result is having both files and folders. How do i differentiate between the files and folders?

Sample request

Endpoint - https://graph.microsoft.com/v1.0/$batch

Body -
{
"requests": [
{
"url": "/search/query",
"method": "POST",
"id": "1",
"body": {
"requests": [
{
"entityTypes": [
"driveItem"
],
"query": {
"queryString": "test document"
},
"from": 0,
"size": 500
}
]
},
"headers": {
"Content-Type": "application/json"
}
}
]
}

Sample Response -

"hits": [
{
"hitId": "01G7HBIV4QOXJ3QMEMWFEZLWD3OVWEUC7N",
"rank": 1,
"summary": "<c0>test</c0> <ddd/>",
"resource": {
"@odata.type": "#microsoft.graph.driveItem",
"size": 4678,
"id": "01G7HBIV4QOXJ3QMEMWFEZLWD3OVWEUC7N",
"createdDateTime": "2022-01-03T03:50:44Z",
"lastModifiedDateTime": "2022-01-03T03:50:44Z",
"name": "test12.xlsx",
"webUrl": "https://xyz.sharepoint.com/personal/xyz/Documents/TestBatchingFolder/test12.xlsx",
"fileSystemInfo": {
"createdDateTime": "2022-01-03T03:50:44Z",
"lastModifiedDateTime": "2022-01-03T03:50:44Z"
},
"createdBy": {
"user": {
"displayName": "xyz",
"email": "xyz.onmicrosoft.com"
}
},
"lastModifiedBy": {
"user": {
"displayName": "xyz",
"email": "xyz@jaswant .onmicrosoft.com"
}
},
"parentReference": {
"driveId": "xyz",
"id": "xyz",
"siteId": "xyz.sharepoint.com,9b7a4044-3da1-43f4-90d0-b71144f97aae,e9adba9f-a7b2-4a80-a288-60a5bdee9086",
"sharepointIds": {
"listId": "76946dc1-0bfa-48e4-a3a4-977be9f2172d",
"listItemId": "2",
"listItemUniqueId": "B8D37590-8C30-49B1-95D8-7B756C4A0BED"
}
}
}
},
{
"hitId": "01RRXSZS4ESTNUK4WNTBAJHN4OC6RULMZ4",
"rank": 2,
"summary": "",
"resource": {
"@odata.type": "#microsoft.graph.driveItem",
"size": 0,
"id": "01RRXSZS4ESTNUK4WNTBAJHN4OC6RULMZ4",
"createdDateTime": "2022-01-06T23:01:10Z",
"lastModifiedDateTime": "2022-01-06T23:01:10Z",
"name": "Test",
"webUrl": "https://xyz/sites/CustomerSuccess/Shared Documents/Test",
"fileSystemInfo": {
"createdDateTime": "2022-01-06T23:01:10Z",
"lastModifiedDateTime": "2022-01-06T23:01:10Z"
},
"createdBy": {
"user": {
"displayName": "xyz",
"email": "xyz@jaswant .onmicrosoft.com"
}
},
"lastModifiedBy": {
"user": {
"displayName": "xyz",
"email": "xyz@jaswant .onmicrosoft.com"
}
},
"parentReference": {
"driveId": "xyz",
"id": "xyz",
"siteId": "xyz.sharepoint.com,94166574-cd89-4a54-95c4-706daf206b6a,bae9cc2e-6dc8-45ee-906a-9e800af0267e",
"sharepointIds": {
"listId": "ae552a53-63c8-4bc8-9709-031391930c60",
"listItemId": "35",
"listItemUniqueId": "45db9484-cd72-4098-93b7-8e17a345b33c"
}
}
}
}
]

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,863 questions
0 comments No comments
{count} votes

Accepted answer
  1. Srinivasa Rao Darna 6,696 Reputation points Microsoft Vendor
    2022-01-10T12:11:48.017+00:00

    Hi @Priya Shrivastava ,

    Can you try isDocument=true in query string to get only files, refer to documentation use-filters-in-search-queries
    Below is the sample request,

    {  
        "requests": [  
            {  
                "url": "/search/query",  
                "method": "POST",  
                "id": "1",  
                "body": {  
                    "requests": [  
                        {  
                            "entityTypes": [  
                                "driveItem"  
                            ],  
                            "query": {  
                                "queryString": "test document AND isDocument=true"  
                            },  
                            "from": 0,  
                            "size": 500  
                        }  
                    ]  
                },  
                "headers": {  
                    "Content-Type": "application/json"  
                }  
            }  
        ]  
    }  
    

    Here is the example screenshot of response

    163632-filesonlyresponse.jpg

    Hope this helps.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have further questions about this answer, please click "Comment".


1 additional answer

Sort by: Most helpful
  1. Brian T. Jackett 1 Reputation point Microsoft Employee
    2022-01-08T04:09:46.873+00:00

    Can you try querying for entityType of "listItem" and then add "fields" filter to include "contentclass". See below sample.

    {
    "requests": [
    {
    "entityTypes": [
    "listItem"
    ],
    "query": {
    "queryString": "contoso"
    },
    "from": 0,
    "fields": [
    "title",
    "webUrl",
    "contentclass"
    ],
    "size": 5
    }
    ]
    }