6
votes

I'm trying to add some new text to a page knowing the page id referencing this https://docs.microsoft.com/en-us/graph/onenote-update-page

I just want to add a new text box. It seems like it is just another div to the html. How do you just add a div into the body?

I want to insert a new text box that says "hello world".

I've tried the code on that page and all it does is add to existing text boxes.

I'm using python requests.

3

3 Answers

0
votes

To add a text box

PATCH https://graph.microsoft.com/v1.0/me/onenote/notebooks/pages/{page-id}/content

Content-Type: application/json Authorization: Bearer {token}

[{'target':''div:{GUID}{int}','action':'append', 'content':'<div><p>hello world</p></div>'}]

This creates a new textbox but within the selected div on the page. Repeating the process then seems to add to this newly created textbox not a second textbox.

If you want to position the text box on the page and in a separate div (as far as I can tell) then you will need to capture the entire page and replace the page with all the old content and add something like the following

<div style="position:absolute;top:175px;left:100px"><p>Hello World in an absolute positioned div.</p></div>

You will also need to add this to the body definition

<body data-absolute-enabled="true">

0
votes

I think you may not need to add a new "div" but actually, you need to only insert a new paragraph as you said you only need a new text box. So, try the following code and revert the feedback:

Solution #1

headers={'Authorization': 'Bearer ' + token,'Content-Type': 'application/json' }
content_end_point='https://graph.microsoft.com/v1.0/me/onenote/notebooks/pages/{page-id}/content'

data= [{
    'target':'body',
    'action':'append',
    'content':'<p>Hello World!</p>'
  }]

result=requests.patch(endpoint,headers=headers,json=data)
print(result.text)

Solution #2

headers={'Authorization': 'Bearer ' + token,'Content-Type': 'application/json' }
content_end_point='https://graph.microsoft.com/v1.0/me/onenote/notebooks/pages/{page-id}/content'

data= [{
    'target':'body',
    'action':'append',
    'content':'<div><p>Hello World!</p></div>'
  }]

result=requests.patch(endpoint,headers=headers,json=data)
print(result.text)

0
votes

After fiddling with this, I am beginning to think that this is not possible. Ideally, one would want to insert a sibling div (not a child), but according to the documentation on supported elements and actions for updating OneNote pages, adding a sibling div at the top-level is not allowed. It is possible to add a sibling div within another div, though.


My naive attempt initially was to target the first div of the page with "target": "body" and use "action": "insert" to insert a sibling element. This, however, is not allowed by the API.

PATCH https://graph.microsoft.com/v1.0/me/onenote/pages/{id}/content

with this request body:

[
  {
    "target": "body",
    "action": "insert",
    "content": "<div>I am in a new text box.</div>"
  }
]

See the attributes for JSON change objects for documentation on keys and values in JSON object.