在驱动器内搜索 DriveItems
本文内容
命名空间:microsoft.graph
在项层次结构中搜索与查询匹配的项。可以在文件夹层次结构、整个驱动器或与当前用户共享的文件中进行搜索。
Permissions
要调用此 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
注意: 此方法不支持 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}/drive/root/search(q='{search-text}')
可选的查询参数
此方法支持使用 $expand、$select、$skipToken、$top 和 $orderby OData 查询参数 自定义响应。
函数参数
参数
类型
说明
q
字符串
用来搜索项目的查询文本。可以跨多个字段(包括文件名、元数据和文件内容)与值相匹配。
示例
请求
下面的示例在登录用户的驱动器项的多个字段中搜索“Contoso Project”的匹配项。
GET /me/drive/root/search(q='Contoso Project')
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var search = await graphClient.Me.Drive.Root
.Search("Contoso Project")
.Request()
.GetAsync();
有关如何将 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 文档 。
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/v1.0/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me/drive/root/search(q='Contoso Project')"]]];
[urlRequest setHTTPMethod:@"GET"];
MSURLSessionDataTask *meDataTask = [httpClient dataTaskWithRequest:urlRequest
completionHandler: ^(NSData *data, NSURLResponse *response, NSError *nserror) {
NSError *jsonError = nil;
MSCollection *collection = [[MSCollection alloc] initWithData:data error:&jsonError];
MSGraphDriveItem *driveItem = [[MSGraphDriveItem alloc] initWithDictionary:[[collection value] objectAtIndex: 0] error:&nserror];
}];
[meDataTask execute];
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
DriveItemSearchCollectionPage search = graphClient.me().drive().root()
.search(DriveItemSearchParameterSet
.newBuilder()
.withQ("Contoso Project")
.build())
.buildRequest()
.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')
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var search = await graphClient.Me.Drive
.Search("Contoso Project")
.Request()
.GetAsync();
有关如何将 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 文档 。
MSHTTPClient *httpClient = [MSClientFactory createHTTPClientWithAuthenticationProvider:authenticationProvider];
NSString *MSGraphBaseURL = @"https://graph.microsoft.com/v1.0/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[MSGraphBaseURL stringByAppendingString:@"/me/drive/search(q='Contoso Project')"]]];
[urlRequest setHTTPMethod:@"GET"];
MSURLSessionDataTask *meDataTask = [httpClient dataTaskWithRequest:urlRequest
completionHandler: ^(NSData *data, NSURLResponse *response, NSError *nserror) {
NSError *jsonError = nil;
MSCollection *collection = [[MSCollection alloc] initWithData:data error:&jsonError];
MSGraphDriveItem *driveItem = [[MSGraphDriveItem alloc] initWithDictionary:[[collection value] objectAtIndex: 0] error:&nserror];
}];
[meDataTask execute];
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
DriveSearchCollectionPage search = graphClient.me().drive()
.search(DriveSearchParameterSet
.newBuilder()
.withQ("Contoso Project")
.build())
.buildRequest()
.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"
}
错误响应
请参阅错误响应 ,详细了解错误返回方式。