命名空间:microsoft.graph
在项目层次结构中搜索与查询匹配的项目。
可以在文件夹层次结构、整个驱动器或与当前用户共享的文件内执行搜索。
权限
为此 API 选择标记为最低特权的权限。
只有在应用需要它时,才使用更高的特权权限。 有关委派权限和应用程序权限的详细信息,请参阅权限类型。 要了解有关这些权限的详细信息,请参阅 权限参考。
| 权限类型 |
最低特权权限 |
更高特权权限 |
| 委派(工作或学校帐户) |
Files.Read |
Files.Read.All、Files.ReadWrite、Files.ReadWrite.All、Sites.Read.All、Sites.ReadWrite.All |
| 委派(个人 Microsoft 帐户) |
Files.Read |
Files.Read.All、Files.ReadWrite、Files.ReadWrite.All |
| 应用程序 |
Files.Read.All |
Files.ReadWrite.All、Sites.Read.All、Sites.ReadWrite.All |
注意: 此方法不支持 Sites.Selected 应用程序权限。
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 | userPrincipalName}/drive/root/search(q='{search-text}')
可选的查询参数
此方法支持 、$expand$select、、 $skipToken$top和 $orderbyOData 查询参数来自定义响应。
函数参数
| 参数 |
类型 |
说明 |
| q |
字符串 |
用来搜索项目的查询文本。 可以跨多个字段(包括文件名、元数据和文件内容)与值相匹配。 |
| 名称 |
说明 |
| Authorization |
持有者 {token}。 必填。 详细了解 身份验证和授权。 |
示例
请求
下面的示例在登录用户的驱动器项的多个字段中搜索“Contoso Project”的匹配项。
GET /me/drive/root/search(q='Contoso Project')
// Code snippets are only available for the latest version. Current version is 5.x
// To initialize your graphClient, see https://dori-uw-1.kuma-moon.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Drives["{drive-id}"].Items["{driveItem-id}"].SearchWithQ("{q}").GetAsSearchWithQGetResponseAsync();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
//other-imports
)
// To initialize your graphClient, see https://dori-uw-1.kuma-moon.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
q := "{q}"
search, err := graphClient.Drives().ByDriveId("drive-id").Items().ByDriveItemId("driveItem-id").SearchWithQ(&q).GetAsSearchWithQGetResponse(context.Background(), nil)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
var result = graphClient.drives().byDriveId("{drive-id}").items().byDriveItemId("{driveItem-id}").searchWithQ("{q}").get();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
const options = {
authProvider,
};
const client = Client.init(options);
let search = await client.api('/me/drive/root/search(q='Contoso Project')')
.get();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
<?php
use Microsoft\Graph\GraphServiceClient;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$result = $graphServiceClient->drives()->byDriveId('drive-id')->items()->byDriveItemId('driveItem-id')->searchWithQ('{q}', )->get()->wait();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
Import-Module Microsoft.Graph.Files
Search-MgDriveItem -DriveId $driveId -DriveItemId $driveItemId
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
# To initialize your graph_client, see https://dori-uw-1.kuma-moon.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
result = await graph_client.drives.by_drive_id('drive-id').items.by_drive_item_id('driveItem-id').search_with_q("{q}").get()
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
响应
此方法返回一个对象,该对象包含与搜索条件相匹配的 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='Contoso Project')
// Code snippets are only available for the latest version. Current version is 5.x
// To initialize your graphClient, see https://dori-uw-1.kuma-moon.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Drives["{drive-id}"].SearchWithQ("{q}").GetAsSearchWithQGetResponseAsync();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
//other-imports
)
// To initialize your graphClient, see https://dori-uw-1.kuma-moon.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
q := "{q}"
search, err := graphClient.Drives().ByDriveId("drive-id").SearchWithQ(&q).GetAsSearchWithQGetResponse(context.Background(), nil)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
var result = graphClient.drives().byDriveId("{drive-id}").searchWithQ("{q}").get();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
const options = {
authProvider,
};
const client = Client.init(options);
let search = await client.api('/me/drive/search(q='Contoso Project')')
.get();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
<?php
use Microsoft\Graph\GraphServiceClient;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$result = $graphServiceClient->drives()->byDriveId('drive-id')->searchWithQ('{q}', )->get()->wait();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
# To initialize your graph_client, see https://dori-uw-1.kuma-moon.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
result = await graph_client.drives.by_drive_id('drive-id').search_with_q("{q}").get()
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
响应
从驱动器资源中搜索时的响应可能包括驱动器外部的项目(与当前用户共享的项目)。
这些项包括 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"
}
错误响应
有关如何返回错误的详细信息,请参阅 错误响应。