首页 > 解决方案 > InvalidRequest 在驱动器之间移动 DriveItem

问题描述

我正在尝试将DriveItem位于父 SharePoint 网站的根驱动器中的一个移动到子子网站。

父站点: mydomain.sharepoint.com/sites/Graph- 列表:文档

子站点 mydomain.sharepoint.com/sites/Graph/SubwebA- 列表:文档

我正在使用 Microsoft Graph SDK v3.25.0:

var client = GetGraphClient();
var destDriveItem = new DriveItem
{
  ParentReference = new ItemReference
  {
    DriveId = destDriveID,
    Id = destDriveFolderID
  }
};

var response = await client
  .Drives[driveID]
  .Items[sourceDriveItemID]
  .Request()
  .UpdateAsync(destDriveItem);

所有 ID 均有效,但UpdateAsync返回此错误:

ServiceException: Code: invalidRequest
Message: Requested move requires an async response, add 'Prefer: respond-async' to allow
Inner error:
        AdditionalData:
        date: 2021-03-04T15:44:38
        request-id: 2aa656e2-fc4b-4314-846b-b62680a15ece
        client-request-id: 2aa656e2-fc4b-4314-846b-b62680a15ece
        ClientRequestId: 2aa656e2-fc4b-4314-846b-b62680a15ece

   at Microsoft.Graph.HttpProvider.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
   at Microsoft.Graph.BaseRequest.SendRequestAsync(Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
   at Microsoft.Graph.BaseRequest.SendAsync[T](Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
   at Microsoft.Graph.DriveItemRequest.UpdateAsync(DriveItem driveItemToUpdate, CancellationToken cancellationToken)
   at GraphToM365Test.Program.MoveDriveItemToDestinationAndBack(String sourceDriveItemId) in C:\Dev\StratusApps\GraphToM365Test\GraphToM365Test\Program.cs:line 166
   at GraphToM365Test.Program.MainAsync(String[] args) in C:\Dev\StratusApps\GraphToM365Test\GraphToM365Test\Program.cs:line 62

如果我在同一站点中移动DriveItem两个(文档库)之间的代码,则相同的代码可以工作。Drives

该问题可能与此GitHub 问题有关(尽管这是为了在同一站点中移动)

标签: c#microsoft-graph-apisharepoint-onlinemicrosoft-graph-sdksmicrosoft-graph-files

解决方案


这是不受支持的操作。从文档中

无法使用此请求在驱动器之间移动项目。

相反,您需要拨打两个电话:

  1. 文件复制到新位置
  2. 从旧位置删除文件
var client = GetGraphClient();

var parentReference = new ItemReference
{
  DriveId = destDriveID,
  Id = destDriveFolderID
};

var copyResponse = await client
  .Drives[driveID]
  .Items[sourceDriveItemID]
  .Copy(name, parentReference)
  .Request()
  .PostAsync();

var deleteResponse = await client
  .Drives[driveID]
  .Items[sourceDriveItemID]
  .Request()
  .DeleteAsync();

推荐阅读