Freewalker
Freewalker

Reputation: 7355

How can I stop MS Graph from wrapping <br /> tags inside a <p>?

I am trying to create a simple OneNote page with line breaks between paras. When I read from the ON Graph API, the structure is like this:

<p style="margin-top:0pt;margin-bottom:0pt">
  First para
</p>
<br />
<p style="margin-top:0pt;margin-bottom:0pt">
  Second para
</p>

However, when I write this exact structure back to the Graph, it wraps the <br /> tag, causing two line breaks to appear instead of one.

<p><br /></p>

How do I convince the MS Graph that I just want a freestanding, not-wrapped <br /> tag, identically to how OneNote adds it when I am typing in the app?

I have tried as many ways to do this as I can think of (add multiple <br /> tags; style the <br /> tag; change the content type) but with no success.

My current method is to add the <br /> tags inside the preceding paragraph, which is an ok hack - the downside is that when I am later working with the text within OneNote, results may be unpredictable. E.g. typing on the empty line then adding a checkbox will add it on the preceding para, since the newline is considered part of the last para:

<p>
  First para
  <br />
</p>
<p>
  Second para
</p>

I believe this must be submitted as HTML since the Graph default is to add line spacing, for some very odd reason - as this does not match ON default behavior when adding pages in-app. So all

tags need margin specified for formatting to be consistent with in-app editing.

Upvotes: 0

Views: 134

Answers (1)

codeye
codeye

Reputation: 627

If I'm understanding the question, it's how to replicate the page using post or patch commands. The solution I use is to create a template page and then get it's structure with
GET https://graph.microsoft.com/v1.0/me/onenote/pages/{id}/content?includeIDs=true[&includeInkML=true]
This will retrieve the page structure with unique paragraph ids that can then be targeted with create or update APIs to replicate the correct page structure
PATCH https://graph.microsoft.com/v1.0/me/onenote/pages/{id}/content

[
  {
    "target": "p:{15cc58cb-da48-430d-0fa8-e30f08437d2a}{28}",
    "action": "replace",
    "content": "<p style=\"margin-top:0pt;margin-bottom:0pt\"><span style=\"\">First para 
</span></p>"
  }
]

Upvotes: 0

Related Questions