2

I am using the Microsoft Graph API to programmatically add content to a OneNote Page on my Office365 OneNote Notebook. For an example page like this: OneNote snip consisting of Title & 2 separate divs/blocks, this is the generated HTML that I get by making a GET to the pages/{myPageID}/content?includeids=true endpoint

<html lang="en-US">
<head>
    <title>Thursday, December 6, 2018</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="created" content="2018-12-06T19:55:00.0000000" />
</head>
<body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">
    <div id="div:{95790d05-168a-4f61-81a7-5c13e3124069}{4}" style="position:absolute;left:48px;top:115px;width:624px">
        <p id="p:{95790d05-168a-4f61-81a7-5c13e3124069}{10}" style="margin-top:0pt;margin-bottom:0pt">It is a jolly good day</p>
    </div>
    <div id="div:{3a6b78e9-2af8-4a5b-abf8-09871fb6eec5}{20}" style="position:absolute;left:44px;top:180px;width:624px">
        <p id="p:{3a6b78e9-2af8-4a5b-abf8-09871fb6eec5}{24}" style="margin-top:0pt;margin-bottom:0pt">Lets do this shall we</p>
    </div>
</body>
</html> 

I now try adding a new div/block by making the following PATCH using the requests library in Python. The myPageID and access_token are collected previously by making GET requests to the graph API:

patchPageURL='https://graph.microsoft.com/v1.0/users/'+userID+'/onenote/pages/'+myPageID+'/content?includeIDs=true'
patchPageHeaders={
    'Content-Type':'application/json', 
    'Authorization':'Bearer '+access_token
}
patchPageBody=[{
    'target':'div:{3a6b78e9-2af8-4a5b-abf8-09871fb6eec5}{20}',
    'action':'insert',
    'position':'after',
    'content':'<div><p> And God said let there be new blocks </p></div>'
}]
patchResp = requests.patch(patchPageURL, headers=patchPageHeaders, json=patchPageBody)

However, I end up with a 400 error as a response to my PATCH request with the following code and message:

code: 20135
message: The entity type is not supported for this operation.

If I change the opening div tag for the content to something like <div data-id="new-div">, I still receive the same 400 error, code 20135.

I am following the instructions from this documentation here. What subtlety is being missed here? There are no authentication or other similar errors. The PATCH works fine if I change the target to body and the action to append but that doesn't make a new div/block and only adds the content as a child of the first div in the page.

Is there a straightforward way to add new blocks/divs as a sibling of existing blocks or even just something as simple as adding a new block at the end of the OneNote page?

  • 1
    Welcome to Stack Overflow! I strongly recommend reading ["How do I ask a good question?"](https://stackoverflow.com/help/how-to-ask) for some tips on getting started. Without seeing the code you're using to call and the actual response, it will be nearly impossible to help here. – Marc LaFleur Dec 07 '18 at 19:36
  • Thanks for the comment, Marc. I have updated the post to include the relevant code snippet being used for making the `PATCH` request to update the page. I didn't include them earlier because I thought it was boilerplate that is standard for all requests to the API – Anurag Saha Roy Dec 09 '18 at 08:23
  • What happens if you insert `
    And God said let there be new blocks
    ` (i.e. without a child `

    ` element)?

    – Marc LaFleur Dec 10 '18 at 19:04
  • The same `400` response with error code `20135`. – Anurag Saha Roy Dec 10 '18 at 19:55
  • You may want to do a trace of the call with Fiddler and see exactly what Python is sending down the wire. More often than not the platform is being "helpful" and automatically setting values you're not expecting. – Marc LaFleur Dec 11 '18 at 15:21
  • I have reproduced the same error using Microsoft Graph Explorer (with and without a child `

    ` and the `div-id`). It gives the exact same `400` error with `code: 20135`. And I just rechecked the actual request with Fiddler. There're no additional fields being set by the Python library.

    Just to clarify, is the `PATCH` request described above in the OP the **intended** way of creating a new div or is there something that I am missing in the documentation?

    – Anurag Saha Roy Dec 12 '18 at 10:19
  • It has been a while since I've worked with this API, but I share your expectations in terms of how to add a new `div`. I just wanted to make sure it wasn't something trivial (i.e. it didn't like adding both a `div` and a `p` at the same time) but clearly that isn't it. I will see if I can find anything in my notes – Marc LaFleur Dec 12 '18 at 16:02
  • I think I found it but before putting it in an answer, I'd like you to check [this section](https://learn.microsoft.com/en-us/graph/onenote-input-output-html#non-contributing-divs) in the docs. After reading it, I believe I recall running into this once. Basically, OneNote will automatically handle the `div` so you only need to insert `

    And God said let there be new blocks

    ` and it will determine if/when it should wrap that in a `
    ` or include it in an existing one.
    – Marc LaFleur Dec 12 '18 at 16:08
  • Turns out any `insert` action with whatsoever HTML content on a `div` element `target` returns the same `code: 20135` error (with or without including a `div` tag in the content. Which is basically what the error `message: "The entity type is not supported for this operation."` implies. I will just accept this as a bug in the OneNote API then since there is clearly no way to `PATCH` new `divs` as siblings of existing `divs` – Anurag Saha Roy Dec 13 '18 at 02:15

0 Answers0