在驱动器中搜索 DriveItem

在项目层次结构中搜索与查询匹配的项目。 可以在文件夹层次结构、整个驱动器或与当前用户共享的文件内执行搜索。

权限

调用此 API 需要以下权限之一。 若要了解详细信息,包括如何选择权限的信息,请参阅权限

权限类型 权限(从最低特权到最高特权)
委派(工作或学校帐户) Files.Read、Files.ReadWrite、Files.Read.All、Files.ReadWrite.All、Sites.Read.All、Sites.ReadWrite.All
委派(个人 Microsoft 帐户) Files.Read、Files.ReadWrite、Files.Read.All、Files.ReadWrite.All
应用程序 Files.Read.All、Files.ReadWrite.All、Sites.Read.All、Sites.ReadWrite.All

HTTP 请求

GET /drives/{drive-id}/root/search(q='{search-text}')
GET /groups/{group-id}/drive/root/search(q='{search-text}')
GET /me/drive/root/search(q='{search-text}')
GET /sites/{site-id}/drive/root/search(q='{search-text}')
GET /users/{user-id}/drive/root/search(q='{search-text}')

可选的查询参数

此方法支持 、$expand$select、、 $skipToken$top$orderbyOData 查询参数来自定义响应。

函数参数

名称 说明
q string 用来搜索项目的查询文本。 可以跨多个字段(包括文件名、元数据和文件内容)与值相匹配。

示例

请求

下面是一个请求搜索当前用户的 OneDrive 示例

GET /me/drive/root/search(q='{search-query}')

响应

此方法返回一个对象,该对象包含与搜索条件相匹配的 DriveItem 集合。 如果未找到任何项目,则返回一个空集合。

如果匹配项太多,将对响应分页,并且 @odata.nextLink 属性将包含指向下一页结果的 URL。 可以使用 $top 查询参数来指定页中的项目数。

HTTP/1.1 200 OK
Content-type: application/json

{
    "value": [
      {
        "id": "0123456789abc!123",
        "name": "Contoso Project",
        "folder": {},
        "searchResult": { "onClickTelemetryUrl": "https://bing.com/0123456789abc!123" }
      },
      {
        "id": "0123456789abc!456",
        "name": "Contoso Project 2016",
        "folder": {},
        "searchResult": { "onClickTelemetryUrl": "https://bing.com/0123456789abc!456" }
      }
    ],
    "@odata.nextLink": "https://graph.microsoft.com/v1.0/me/drive/root/search(query='contoso project')&skipToken=1asdlnjnkj1nalkm!asd"
}

搜索用户可以访问的项目

除了在驱动器中搜索项目外,你的应用程序还可以进行更广泛的搜索,以便包含与当前用户共享的项目。 若要扩大搜索范围,请使用 驱动器 资源中的搜索方法。

示例

GET /me/drive/search(q='{search-query}')

响应

驱动器资源中搜索时的响应可能包括驱动器外部的项目(与当前用户共享的项目)。 这些项目将包括 remoteItem 方面,以指示它们存储在目标驱动器外部。

HTTP/1.1 200 OK
Content-type: application/json

{
    "value": [
      {
        "id": "0123456789abc!123",
        "name": "Contoso Project",
        "folder": {},
        "searchResult": { "onClickTelemetryUrl": "https://bing.com/0123456789abc!123" },
        "remoteItem": { "id": "!23141901", "parentReference": { "driveId": "s!1020101jlkjl12lx" } }
      },
      {
        "id": "0123456789abc!456",
        "name": "Contoso Project 2016",
        "folder": {},
        "searchResult": { "onClickTelemetryUrl": "https://bing.com/0123456789abc!456" }
      }
    ],
    "@odata.nextLink": "https://graph.microsoft.com/v1.0/me/drive/root/search(query='contoso project')&skipToken=1asdlnjnkj1nalkm!asd"
}

注解

注意:在 OneDrive for Business 和 SharePoint 中,在文件夹层次结构下搜索时,可能不会在结果中返回图像文件类型。

错误响应

请参阅错误响应,详细了解错误返回方式。