如何使用 MSGraph 查询 API 在 OneDrive 中搜索文件

问题描述 投票:0回答:1

使用 Graph Explorer 我可以列出我的 oneDrive 的“Brendan”目录中的文件。 例如下面的 API 调用

https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/children

正确地将 2 个文件列为

"name": "bren1.txt"
,和
"name": "test.pdf"

但是,如果我尝试使用此 API 来使用查询 API https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/children/query 用这身材

{
    "requests": [
        {
            "entityTypes": [
                "driveItem"
            ],
            "query": {
                "queryString": "bren"
            }
        }
    ]
}

我明白了

 {
    "error": {
        "code": "itemNotFound",
        "message": "The resource could not be found.",
        "innerError": {
            "date": "2023-04-25T06:03:59",
            "request-id": "e3c688e1-dc08-4302-acdb-1b133a83bae3",
            "client-request-id": "fc6fc88d-38ee-451b-3ada-1914d2f96e91"
        }
    }
}

我找不到关于如何使用 MSGraph 查询 API 在 OneDrive 上的目录 (driveItems) 中搜索文件的具体示例。 我哪里错了?

另外

?$filter
似乎也偶尔会起作用,例如:

https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/children?$filter=startswith(name,'bren')

正确返回

"name": "bren1.txt"

但是

https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/children?$filter=endswith(name,'txt')
https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/children?$filter=contains(name,'bren')
都返回

{
    "error": {
        "code": "itemNotFound",
        "message": "Item not found",
        "innerError": {
            "date": "2023-04-25T06:13:56",
            "request-id": "28cee436-ec08-4107-8947-380b387ada7d",
            "client-request-id": "6d754b02-aaef-7360-b3ca-c0341a9b6f19"
        }
    }
}

我也试过

https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/children?$search="name:bren1.txt"
https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/children?$search="bren1.txt"

两者都忽略搜索参数并返回所有内容。

再次我哪里错了?

microsoft-graph-api onedrive graph-explorer
1个回答
0
投票

我更喜欢使用

search(q='{search-text}')
来搜索文件。

GET https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/search(q='bren1.txt')

上面的查询将在您的 OneDrive 的“Brendan”目录中搜索项目。它在文件名、元数据和文件内容中搜索值。

我认为

$filter
查询被忽略了。

第二个选项是使用

/search/query
端点

POST https://graph.microsoft.com/v1.0/search/query

{
    "requests": [
        {
            "entityTypes": [
                "driveItem"
            ],
            "query": {
                "queryString": "filename:bren1.txt AND path:\"https://tenant-my.sharepoint.com/personal/xxx/Documents/Brendan\""
            }
        }
    ]
}

您可以按文件名过滤项目,并将查询范围限定在 OneDrive 或站点内的特定文件夹。

文档:

搜索文件

在搜索查询中过滤

© www.soinside.com 2019 - 2024. All rights reserved.