Dheeraj Kesri
Dheeraj Kesri

Reputation: 15

Document uploaded to MS Teams using graph API gets corrupted

I am trying to upload a document to Microsoft Teams using Microsoft Graph (beta version), but the document gets corrupted after a successful upload.

Using Graph, I'm first creating an Group, creating a Team based on the Group, adding some Team Members and finally uploading a document to the default channel.

All works fine except the uploaded document gets corrupted and the Office Online editor is not able to open it. We can however download the file and open in Microsoft Word after correcting the file.

Below is the code that I'm using for document upload->

FileInfo fileInfo = 
    new FileInfo(@"F:\Projects\TestProjects\MSTeamsSample\MSTeamsSample\Files\Test File.docx");

var bytes = System.IO.File.ReadAllBytes(fileInfo.FullName);
var endpoint = $"https://graph.microsoft.com/beta/groups/{groupId}/drive/items/root:/General/{fileInfo.Name}:/content";

var fileContent = new ByteArrayContent(bytes);
fileContent.Headers.ContentType = 
    MediaTypeHeaderValue.Parse("application/octet-stream");

var requestContent = new MultipartFormDataContent();
requestContent.Add(fileContent, "File", fileInfo.Name);

var request = new HttpRequestMessage(HttpMethod.Put, endpoint);
request.Headers.Authorization = 
    new AuthenticationHeaderValue("Bearer", "<Access Token>");
request.Content = requestContent;
var client = new HttpClient();
var response = client.SendAsync(request).Result;

I tried changing content type to application/vnd.openxmlformats-officedocument.wordprocessingml.document but no luck. I don't understand what could be wrong here. The code is pretty straight forward, based on the this documentation. Any help will be highly appreciated.

Upvotes: 1

Views: 1845

Answers (1)

Wajeed Shaikh
Wajeed Shaikh

Reputation: 3168

Please try this:

        var filePath = @"F:\Projects\TestProjects\MSTeamsSample\MSTeamsSample\Files\Test File.docx";
        var fileName = Path.GetFileName(filePath);
        var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        var endpoint = $"https://graph.microsoft.com/beta/groups/{groupId}/drive/items/root:/General/{fileName}:/content";

        using (var client = new HttpClient())
        {
            using (var content = new StreamContent(fileStream))
            {
                content.Headers.Add("Content-Type", MimeMapping.GetMimeMapping(fileName));

                // Construct the PUT message towards the webservice
                using (var request = new HttpRequestMessage(HttpMethod.Put, endpoint))
                {
                    request.Content = content;

                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokenResponse.Token);

                    // Request the response from the webservice
                    using (var response = await client.SendAsync(request))
                    {
                        // Check the response.
                    }
                }
            }
        }

I am able to see Word document in Microsoft Teams editor.

Upvotes: 1

Related Questions