如何使用Graph API DriveItem仅搜索根目录文件(避免递归检索)
问题
需要查询指定SharePoint站点(ID:00000000-1111-2222-3333-444444444444)的Shared Documents根目录中名称包含“Matrix”的文件,但原请求会递归检索所有子文件夹内的内容:
https://graph.microsoft.com/v1.0/sites/MyDomain.sharepoint.com,00000000-1111-2222-3333-444444444444/drive/search(q='Matrix')
尝试将路径改为/drive/root/search无效果,添加$filter=parentReference/path eq '/drive/root'时收到服务端错误:
"Only createdDateTime,remoteItem.shared.sharedBy.group.id,remoteItem.shared.sharedBy.user.id is supported for filtering"
可行解决方案
方案1:直接访问根目录子项并筛选
使用drive/root/children接口直接获取根目录的直接子项,结合$search和$filter实现需求:
https://graph.microsoft.com/v1.0/sites/MyDomain.sharepoint.com,00000000-1111-2222-3333-444444444444/drive/root/children?$search="Matrix"&$filter=file ne null
children:仅返回根目录的直接子元素,不会递归子文件夹$search="Matrix":匹配名称或内容包含“Matrix”的项$filter=file ne null:过滤掉文件夹,只保留文件
方案2:客户端过滤搜索结果
若必须使用drive/search接口,可先获取所有搜索结果,再在客户端筛选parentReference.path等于/drive/root的文件:
- 发起原搜索请求:
https://graph.microsoft.com/v1.0/sites/MyDomain.sharepoint.com,00000000-1111-2222-3333-444444444444/drive/search(q='Matrix')
- 遍历返回的结果集,仅保留满足
parentReference.path === "/drive/root"且file字段存在的项。
内容的提问来源于stack exchange,提问作者velkoon
相关产品推荐
相关产品推荐

