I have seen many articles about this but all of them are either incomplete or do not answer my question. Using C#
and the OneNote Interop, I would like to simply write text to an existing OneNote 2013 Page. Currently I have a OneNote Notebook, with a Section titled "Sample_Section
" and a Page called "MyPage"
.
I need to be able to use C#
code to write text to this Page, but I cannot figure out how or find any resources to do so. I have looked at all of the code examples on the web and none answer this simple question or are able to do this. Also many of the code examples are outdated and break when attempting to run them.
I used the Microsoft
code sample that shows how to change the name of a Section but I cannot find any code to write text to a Page
. There is no simple way to do this that I can see. I have taken a lot of time to research this and view the different examples online but none are able to help.
I have already viewed the MSDN
articles on the OneNote
Interop
as well. I vaguely understand how the OneNote
Interop
works through XML
but any extra help understanding that would also be appreciated. Most importantly I would really appreciate a code example that demonstrates how to write text to a OneNote
2013 Notebook Page.
I have tried using this Stack Overflow answer: Creating new One Note 2010 page from C#
However, there are 2 things about this solution that do not answer my question:
1) The marked solution shows how to create a new page, not how to write text to it or how to populate the page with any information.
2) When I try to run the code that is marked as the solution, I get an error at the following line:
var node = doc.Descendants(ns + nodeName).Where(n => n.Attribute("name").Value == objectName).FirstOrDefault();
return node.Attribute("ID").Value;
The reason being that the value of "node" is null, any help would be greatly appreciated.
This is just what I've gleaned from reading examples on the web (of course, you've already read all of those) and peeking into the way OneNote stores its data in XML using ONOMspy (http://blogs.msdn.com/b/johnguin/archive/2011/07/28/onenote-spy-omspy-for-onenote-2010.aspx).
If you want to work with OneNote content, you'll need a basic understanding of XML. Writing text to a OneNote page involves creating an outline element, whose content will be contained in
OEChildren
elements. Within anOEChildren
element, you can have many other child elements representing outline content. These can be of typeOE
orHTMLBlock
, if I'm reading the schema correctly. Personally, I've only ever usedOE
, and in this case, you'll have anOE
element containing aT
(text) element. The following code will create an outline XElement and add text to it:Here's what the actual XML you're sending to OneNote looks like:
Hope that helps get you started!
Given task can be easily solved using Aspose.Note. Following code creates a new Document and adds text to its title:
If you're using C#, Check out the newer OneNote REST API at http://dev.onenote.com. It already supports creating a new page and has a beta API to patch and add content to an existing page.
I asked the same question on MSDN forums and was given this great answer. Below is a nice, clean example of how to write to OneNote using C# and the OneNote interop. I hope that this can help people in the future.