none
Document uploaded to MS Teams using graph API gets corrupted RRS feed

  • Question

  • Hi,

    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 Microsoft Word opens it up successfully as a new document after correcting it.

    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 Microsoft Graph documentation. Any help will be highly appreciated.

    Monday, May 21, 2018 6:20 AM

Answers