Codesmith
Codesmith

Reputation: 177

Posting Documents to OneNote via new REST API

For some reason, any document I upload to OneNote via the new REST API is corrupt when viewed from OneNote. Everything else is fine, but the file (for example a Word document) isn't clickable and if you try and open is shows as corrupt.

This is similar to what may happen when there is a problem with the byte array, or its in memory, but that doesn't seem to be the case. I use essentially the same process to upload the file bytes to SharePoint, OneDrive, etc. It's only to OneNote that the file seems to be corrupt.

Here is a simplified version of the C#

HttpRequestMessage createMessage = null;
HttpResponseMessage response = null;

using (var streamContent = new ByteArrayContent(fileBytes))
{
    streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
    streamContent.Headers.ContentDisposition.Name = fileName;

    createMessage = new HttpRequestMessage(HttpMethod.Post, authorizationUrl)
      {
          Content = new MultipartFormDataContent
            {
                { 
                new StringContent(simpleHtml, 
                System.Text.Encoding.UTF8, "text/html"), "Presentation"
                },

              {streamContent}
            }
      };

    response = await client.SendAsync(createMessage);

    var stream = await response.Content.ReadAsStreamAsync();

    successful = response.IsSuccessStatusCode;
}

Does anyone have any thoughts or working code uploading an actual binary document via the OneNote API via a Windows Store app?

Upvotes: 2

Views: 686

Answers (2)

Codesmith
Codesmith

Reputation: 177

So, the issue was (strangely) the addition of the meta Content Type in the tag sent over in the HTML content that's not shown. The documentation refers to adding a type=[mime type] in the object tag, and since the WinStore example didn't do this (it only adds the mime type to the MediaTypeHeaderValue I removed it and it worked perfectly.

Just changing it to this worked:

<object data-attachment=\"" + fileName + "\" data=\"name:" + attachmentPartName + "\" />

Thanks for pointing me in the right direction with the sample code!

Upvotes: 2

DipakBoyed
DipakBoyed

Reputation: 432

The WinStore code sample contains a working example (method: CreatePageWithAttachedFile) of how to upload an attachment. The slight differences I can think of between the above code snippet and the code sample are that the code sample uploads a pdf file (instead of a document) and the sample uses StreamContent (while the above code snippet uses ByteArrayContent). I downloaded the code sample and locally modified it to use a document file and ByteArrayContent. I was able to upload the attachment and view it successfully. Used the following to get a byte array from a given stream:

   using (BinaryReader br = new BinaryReader(stream))
   {
       byte[] b = br.ReadBytes(Convert.ToInt32(s.Length)); 
   }

The rest of the code looks pretty similar to the above snippet and overall worked successfully for me.

Here are a few more things to consider while troubleshooting the issue:

  • Verify the attachment file itself isn't corrupt in the first place. for e.g. can it be opened without the OneNote API being in the mix?
  • Verify the API returned a 201 Http Status code back and the resulting page contains the attachment icon and allows downloading/viewing the attached file.

Upvotes: 3

Related Questions