MS Graph中如何筛选/drives子项中的非文件夹文件?
我需要通过/drives/{drive-id}/items/{item-id}/children接口获取指定目录下的所有文件(过滤掉文件夹)。目前用$filter=folder ne null可以正常获取仅文件夹,但反转条件时会提示**"The request is not supported by the system."**。我尝试过以下几种筛选条件都无效:
/drives/{drive-id}/items/{item-id}/children?$filter=folder eq null/drives/{drive-id}/items/{item-id}/children?$filter=not(folder ne null)/drives/{drive-id}/items/{item-id}/children?$filter=file ne null—— 该条件无效,因为file属性不支持用于筛选
文件对象示例
{ "createdDateTime": "2023-03-06T14:31:51Z", "eTag": "\"{D1EAF39B-B391-4BFD-926D-A4FE986AEE6E},2\"", "id": "02DDZDZCM36PVNDEND7VFZE3NE92MGVPTO", "lastModifiedDateTime": "2023-02-20T11:03:34Z", "name": "someworkbook.xlsx", "file": { "mimeType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "hashes": { "quickXorHash": "zjhLucU2pN7vTR8p39LRh7W6C0o=" } }, "shared": { "scope": "users" } }
文件夹对象示例
{ "createdDateTime": "2023-03-06T14:31:18Z", "id": "01ADZDZCKYWXT5BK6AQFHIVOACUHRM4BFE", "lastModifiedDateTime": "2023-03-06T14:31:17Z", "name": "Folder A", "folder": { "childCount": 0 }, "searchResult": {} }
可行解决方案
使用$filter=not folder这个筛选条件,对应的请求URL是:
/drives/{drive-id}/items/{item-id}/children?$filter=not folder
原理说明
Microsoft Graph API的OData过滤规则中,对于folder这类复杂类型属性:
folder ne null用于筛选包含folder属性的对象(即文件夹)not folder则是判断对象不包含folder属性,恰好对应文件(文件只会返回file属性,不会有folder字段)
之前尝试的folder eq null报错,是因为Graph API不支持直接对复杂类型使用eq null进行过滤;file ne null无效则是因为file属性不在官方支持的过滤字段列表中,仅folder属性支持这类存在性判断过滤。
内容的提问来源于stack exchange,提问作者Obsam93
相关产品推荐
相关产品推荐

