1

I am trying to move a DriveItem located in the root drive of a parent SharePoint site to a child sub-site.

Parent site: mydomain.sharepoint.com/sites/Graph - List: Documents

Child Site mydomain.sharepoint.com/sites/Graph/SubwebA - List: Documents

I am using the 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);

All IDs are valid but UpdateAsync returns this error:

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

The same code works if I move the DriveItem between two Drives (Document Libraries) in the same site.

The issue is probably related to this GitHub issue (although that is for moving in the same site)

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
Eli
  • 1,315
  • 1
  • 16
  • 29

1 Answers1

1

This is an unsupported action. From the documentation:

Items cannot be moved between Drives using this request.

Instead, you need to make two calls:

  1. Copy the file to the new location
  2. Delete the file from the old location
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();
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • 1) I'm actually able to move files between different document libraries in the same web, so I don't see why they say moving files between Drives isn't supported. 2) Re: the Copy() - It is PostAsync() to end the command - the latest stable for Microsoft.Graph v3.25.0 doesn't have UpdateAsync() on IDriveItemCopyRequest. - Also there is a bug in the SDK for Copy in that the Copy Response is always a null reference so you can't get to the copy monitor to see if it succeeded and what the new DriveItem id is. – Eli Mar 04 '21 at 18:29
  • 1
    Since the documentation clearly states that moving files between two drives is not supported and you're attempt is failing, it seems fairly straightforward. – Marc LaFleur Mar 05 '21 at 02:53