Search code examples
c#sharepoint.net-coremicrosoft-graph-apimicrosoft-graph-files

How to get response header in c# Microsoft graph api request


I am trying to do File Copy operation in c# .net core using Microsoft graph API. It is an asynchronous operation, and by doc, it says it returns a location in the response header to check the status of the operation, Now the issue is I need its response header so that I can check the status of file copy operation but every time I am getting 'null' as value, I have tried following code,

DriveItem response = await graphClient.Sites[siteId].Drive.Items[itemId]
                           .Copy(fileName, parentReference)
                           .Request()
                           .PostAsync();

The driveItem returns null but I think at least it should have returned the additional data-carrying response status and location.

When I use online graph api it just works fine returning response and location, but it doesn't with graph client service.


Solution

  • Apparently it is an issue with msgraph-sdk-dotnet, at least it could be reproduced in 3.8.0 version, the error occurs while deserializing HTTP response. Probably it would be more beneficial to report it as a bug in referenced repository.

    Meanwhile you could consider to construct a request for Copy a DriveItem endpoint and process response (including extracting Location header) as demonstrated below:

    var message = graphClient.Sites[siteId].Drive.Items[itemId]
                    .Copy(fileName, parentReference)
                    .Request()
                    .GetHttpRequestMessage();
    
    message.Method = HttpMethod.Post;
    var body = new DriveItemCopyRequestBody {Name = fileName, ParentReference = parentReference};
    message.Content = new StringContent(graphClient.HttpProvider.Serializer.SerializeObject(body));
    message.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    var response = graphClient.HttpProvider.SendAsync(message).Result;
               
    Console.Write(response.Headers.Location);