命名空间:microsoft.graph
重要
Microsoft Graph /beta 版本下的 API 可能会发生更改。 不支持在生产应用程序中使用这些 API。 若要确定 API 是否在 v1.0 中可用,请使用 版本 选择器。
在项目层次结构中搜索与查询匹配的项目。
可以在文件夹层次结构、整个驱动器或与当前用户共享的文件内执行搜索。
权限
为此 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 |
字符串 |
用来搜索项目的查询文本。 可以跨多个字段(包括文件名、元数据和文件内容)与值相匹配。 |
示例
请求
下面的示例在登录用户的驱动器项的多个字段中搜索“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://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Drives["{drive-id}"].Items["{driveItem-id}"].SearchWithQ("{q}").GetAsSearchWithQGetResponseAsync();
// Code snippets are only available for the latest major version. Current major version is $v0.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
//other-imports
)
// To initialize your graphClient, see https://learn.microsoft.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)
// 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();
const options = {
authProvider,
};
const client = Client.init(options);
let search = await client.api('/me/drive/root/search(q='Contoso Project')')
.version('beta')
.get();
<?php
use Microsoft\Graph\Beta\GraphServiceClient;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$result = $graphServiceClient->drives()->byDriveId('drive-id')->items()->byDriveItemId('driveItem-id')->searchWithQ('{q}', )->get()->wait();
Import-Module Microsoft.Graph.Beta.Files
Search-MgBetaDriveItem -DriveId $driveId -DriveItemId $driveItemId
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph_beta import GraphServiceClient
# To initialize your graph_client, see https://learn.microsoft.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()
响应
此方法返回一个对象,该对象包含与搜索条件相匹配的 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://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Drives["{drive-id}"].SearchWithQ("{q}").GetAsSearchWithQGetResponseAsync();
// Code snippets are only available for the latest major version. Current major version is $v0.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
//other-imports
)
// To initialize your graphClient, see https://learn.microsoft.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)
// 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();
const options = {
authProvider,
};
const client = Client.init(options);
let search = await client.api('/me/drive/search(q='Contoso Project')')
.version('beta')
.get();
<?php
use Microsoft\Graph\Beta\GraphServiceClient;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$result = $graphServiceClient->drives()->byDriveId('drive-id')->searchWithQ('{q}', )->get()->wait();
Import-Module Microsoft.Graph.Beta.Files
Search-MgBetaDrive -DriveId $driveId
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph_beta import GraphServiceClient
# To initialize your graph_client, see https://learn.microsoft.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()
响应
从驱动器资源中搜索时的响应可能包括驱动器外部的项目(与当前用户共享的项目)。
这些项目将包括 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", "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"
}
错误响应
请参阅错误响应,详细了解错误返回方式。