0

I haven't found a way to delete items from a page's content with the onennote API.

Let's say my page's content is

<body>
    <div id="div:{681ddef6-e8ad-0de3-2439-dcc668303696}{34}">
        <p id="p:{f5837c83-d816-4337-ab6d-a52abde869a6}{13}">Delete me</p>
        <p id="p:{1710a020-7fa3-46cb-a8ab-64209696e083}{14}">Keep me/p>
    </div>
</body>

I can build a PATCH request to insert, append, preprend, ... but that are no instructions to delete content.

I tried to replace the paragraph by some empty content:

[{
  'target': 'p:{f5837c83-d816-4337-ab6d-a52abde869a6}{13}',
  'action': 'replace',
  'content': ' '
},
]

but this leads to an error:

{
  "error":{
    "code":"20121","message":"The PATCH argument $content:  is invalid.","@api.url":"http://aka.ms/onenote-errors#C20121"
  }
}

Any suggestion ? Thanks.

lvr123
  • 524
  • 6
  • 24

3 Answers3

2

Just looking at this in the microsoft graph, you can replace the content with a HTML line break.

[{
      'target': 'p:{f5837c83-d816-4337-ab6d-a52abde869a6}{13}',
      'action': 'replace',
      'content': '<br/>'
    }]

This seems to delete the corresponding element (although the blank textbox is still selectable)
codeye
  • 607
  • 3
  • 10
  • This works. No neat. But it works. I tried with an HTML comment instead to the `
    `but I couldn't fool it :-/
    – lvr123 Oct 18 '16 at 20:59
  • 2
    Error: The trick with the comment works !!! I tried with an HTML comment instead to the `
    `and it fools the API ;-)
    – lvr123 Oct 18 '16 at 21:19
0

While the API does not support the "DELETE" action in PATCH pages (I'd encourage you to add an entry in our uservoice webpage https://onenote.uservoice.com/forums/245490-onenote-developer-apis), you can replace the entire page with whatever new content you want it to have.

Another option is similar to what Manoj suggested - replace the parent of the element you want to delete with the same content minus the element you want to delete.

Jorge Aguirre
  • 2,787
  • 3
  • 20
  • 27
  • How do you do "you can replace the entire page with whatever new content you want it to have" ? By the way, I already added this to the uservoice (https://onenote.uservoice.com/forums/245490-onenote-developer-apis/suggestions/16644187-api-to-delete-page-content). – lvr123 Oct 18 '16 at 20:57
  • Hi Jorge, given it looks like patching with a HTML comment effectively deletes an element would this not be easy to test and roll into the api? – codeye Oct 19 '16 at 12:51
-1

The content parameter on a PATCH API action of type replace is expected to be valid HTML. It's not valid to specify whitespace as replacement content.

You can explore replacing a higher level element than a single paragraph to achieve your scenario, for example.

  • This is what I do but that's far from ideal. If I add a spwcific surrounding element (e.g. a `div`) : 1) to remove a `p` I'm forced to rebuild the whole `div` each time. 2) if the OneNote user adds content outside of the special `div` we are stuck with the same problem again ! – lvr123 Oct 11 '16 at 14:40
  • Well, this just doesn't work at all. It is not a reliable solution. It's impossible for the user to know whether is adding elements within the special container or not. Or whether his edits does not impact the special container limits or not. – lvr123 Oct 13 '16 at 22:13