OneDrive Copy Item using Microsoft Graph SDK - Gat

2019-05-15 05:23发布

问题:

I am using the MS Graph .net SDK. Attempting to copy a sharepoint document library to another sharepoint document library.

If the file is approximately 38mb, a GatewayTimeout exception is thrown for an unknown error.

Either MS has a bug, or I am doing something incorrectly. Here is my code:

HttpRequestMessage hrm = new HttpRequestMessage(HttpMethod.Post, request.RequestUrl);
hrm.Content = new StringContent(JsonConvert.SerializeObject(request.RequestBody), System.Text.Encoding.UTF8, "application/json");
await client.AuthenticationProvider.AuthenticateRequestAsync(hrm);

HttpResponseMessage response = await client.HttpProvider.SendAsync(hrm);
if (response.IsSuccessStatusCode)
{
     var content = await response.Content.ReadAsStringAsync();
}

}
catch (Microsoft.Graph.ServiceException ex)
{
     throw new Exception("Unknown Error");
}

Anyone see a problem here?

EDIT: Here is my revised code

public static async Task copyFile(Microsoft.Graph.GraphServiceClient client, string SourceDriveId, string SourceItemId, string DestinationDriveId, string DestinationFolderId, string FileName)
{
    try
    {
        var destRef = new Microsoft.Graph.ItemReference()
        {
            DriveId = DestinationDriveId,
            Id = DestinationFolderId
        };
        await client.Drives[SourceDriveId].Items[SourceItemId].Copy(null, destRef).Request().PostAsync();
        //await client.Drives[SourceDriveId].Root.ItemWithPath(itemFileName).Copy(parentReference: dest).Request().PostAsync();
    }
    catch (Microsoft.Graph.ServiceException ex)
    {
        throw new Exception(ex.Message);
    }
}

The above revised code continues to give the same error; however, tonight, it is also occurring on a 13.8mb file that previously had worked fine.

Logically, because the error doesn't occur for smaller files, I think it has something to do with file size.

The response is supposed to be a 202 with location header. See Copy Item in Graph Docs; however, I have never been able to obtain a location header. I suspect that Microsoft Graph is not getting the location header information from the OneDrive API and is therefore throwing a Gateway Timeout error.

回答1:

I believe this is what you're looking for:

await graphClient.Drives["sourceDriveId"]
    .Items["sourceItemId"]
    .Copy(null, new ItemReference()
    {
        DriveId = "destinationDriveId",
            Id = "destinationFolderId"
    })
    .Request()
    .PostAsync();

This will take a given DriveItem and copy it to a folder in another Drive.