5

I'm trying to add some new text to a page knowing the page id referencing this https://learn.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.

J. M. Arnold
  • 6,261
  • 3
  • 20
  • 38
jason
  • 3,811
  • 18
  • 92
  • 147

3 Answers3

0

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">

codeye
  • 607
  • 3
  • 10
  • This doesn't work. It still adds to text in the SAME text box. I want the "hello world" in a completely new text box but on the same page. – jason Feb 13 '21 at 15:44
  • You are right Jason. If you target a div it looks like you can create a separate text box but it is constrained to the div you selected. Seems the only way to add a completely unconstrained textbox is to capture the entire text on the page (which is possible) and then replacing the body with the old text and your new edits. – codeye Feb 14 '21 at 00:36
  • Having just tried that, interestingly the div that I appended the textbox to before, even though it is inside the body is not affected by the replace command - that would appear to be a bug. – codeye Feb 14 '21 at 00:38
  • So are you saying it's not possible? thanks for trying though. I'm going to put a bounty on it later to see if anybody can do it. In the meantime, i'm using a really stupid workaround by manually adding a blank box then using the `replace` function each time. – jason Feb 14 '21 at 00:51
  • It's definitely possible, but it looks to me that the only consistent way of doing it programmatically would be to replace the body of the text each time you want to alter it. I frequently replace the text in individual text boxes but haven't added new text boxes before so I haven't come across this issue - but certainly have banged my head up against other issues! – codeye Feb 14 '21 at 02:51
0

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.

jkr
  • 17,119
  • 2
  • 42
  • 68
0

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)

Soroosh Khodami
  • 1,185
  • 9
  • 17
  • When I tried this, the text was added to the first text box that already existed on the page. It did not create a new textbox, like the OP seems to desire. – jkr Feb 21 '21 at 23:36
  • your code was in the documentation. the documentation clearly states `body` is the first div. `append` would be appending to the first text box, not creating a new one. – jason Feb 22 '21 at 04:12