将 DriveItem 移动到一个新的文件夹
本文内容
命名空间:microsoft.graph
若要将 DriveItem 移动到新的父项,应用程序会请求更新要移动的 DriveItem 的 parentReference 。
这是更新 方法的特殊用例。
你的应用程序可以将以下操作组合到单个请求中:将项目移动到新的容器和更新项目的其他属性。
无法使用这一请求在驱动器 之间移动项目。
权限
要调用此 API,需要以下权限之一。要了解详细信息,包括如何选择权限的信息,请参阅权限 。
权限类型
权限(从最低特权到最高特权)
委派(工作或学校帐户)
Files.ReadWrite、Files.ReadWrite.All、Sites.ReadWrite.All
委派(个人 Microsoft 帐户)
Files.ReadWrite、Files.ReadWrite.All
应用程序
Files.ReadWrite.All、Sites.ReadWrite.All
HTTP 请求
PATCH /drives/{drive-id}/items/{item-id}
PATCH /groups/{group-id}/drive/items/{item-id}
PATCH /me/drive/items/{item-id}
PATCH /sites/{site-id}/drive/items/{item-id}
PATCH /users/{user-id}/drive/items/{item-id}
名称
类型
说明
if-match
String
如果包含此请求标头,且提供的 eTag(或 cTag)与文件夹上的当前 eTag 不匹配,则返回 412 Precondition Failed 响应。
请求正文
在请求正文中,提供 parentReference 属性的新值。请求正文中不包括的现有属性将保留其以前的值,或根据对其他属性值的更改重新计算。为了获得最佳性能,不应包括尚未更改的现有值。
注意: 将项目移动到驱动器的根目录下时,应用程序不能使用 "id:" "root" 语法。
应用程序必须为父引用提供实际的根文件夹 ID。
响应
如果成功,此方法在响应正文中返回 200 OK 响应代码和更新的 DriveItem 资源。
示例
本示例将 {item-id} 指定的项目移动到用户驱动器中 ID 为 new-parent-folder-id 的文件夹中。
PATCH /me/drive/items/{item-id}
Content-type: application/json
{
"parentReference": {
"id": "{new-parent-folder-id}"
},
"name": "new-item-name.txt"
}
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var driveItem = new DriveItem
{
ParentReference = new ItemReference
{
Id = "{new-parent-folder-id}"
},
Name = "new-item-name.txt"
};
await graphClient.Me.Drive.Items["{driveItem-id}"]
.Request()
.UpdateAsync(driveItem);
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
const options = {
authProvider,
};
const client = Client.init(options);
const driveItem = {
parentReference: {
id: '{new-parent-folder-id}'
},
name: 'new-item-name.txt'
};
await client.api('/me/drive/items/{item-id}')
.update(driveItem);
有关如何将 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/items/{item-id}"]]];
[urlRequest setHTTPMethod:@"PATCH"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
MSGraphDriveItem *driveItem = [[MSGraphDriveItem alloc] init];
MSGraphItemReference *parentReference = [[MSGraphItemReference alloc] init];
[parentReference setId:@"{new-parent-folder-id}"];
[driveItem setParentReference:parentReference];
[driveItem setName:@"new-item-name.txt"];
NSError *error;
NSData *driveItemData = [driveItem getSerializedDataWithError:&error];
[urlRequest setHTTPBody:driveItemData];
MSURLSessionDataTask *meDataTask = [httpClient dataTaskWithRequest:urlRequest
completionHandler: ^(NSData *data, NSURLResponse *response, NSError *nserror) {
//Request Completed
}];
[meDataTask execute];
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
DriveItem driveItem = new DriveItem();
ItemReference parentReference = new ItemReference();
parentReference.id = "{new-parent-folder-id}";
driveItem.parentReference = parentReference;
driveItem.name = "new-item-name.txt";
graphClient.me().drive().items("{item-id}")
.buildRequest()
.patch(driveItem);
有关如何将 SDK 添加 到项目并 创建 authProvider 实例的 详细信息,请参阅 SDK 文档 。
响应
以下示例显示了对此移动请求的响应。
HTTP/1.1 200 OK
Content-type: application/json
{
"id": "0123456789abc",
"name": "new-item-name.txt",
"parentReference":
{
"driveId": "11231001",
"path": "/drive/root:/Documents",
"id": "1231203102!1011"
}
}
错误响应
请参阅错误响应 ,详细了解错误返回方式。