I got an email today in which a developer was interested in adding a file to a OneNote page. I pointed him at the OneNote Developer Center on MSDN and wanted to outline (no pun intened) one way of doing this.

First, I started ONOMSpy and looked at the xml of a page that had an attached text file. Here's the XML I saw:

<one:OE creationTime="2012-05-15T01:24:48.000Z" lastModifiedTime="2012-05-15T01:24:48.000Z" objectID="{D83A35A1-EDC1-4D62-BDAE-33D063929871}{60}{B0}" alignment="left" selected="partial">

<one:InsertedFile selected="all" pathCache="C:\Users\John\AppData\Local\Microsoft\OneNote\14.0\OneNoteOfflineCache_Files\ffc255f8-4a1a-41a0-afca-d3b831b0b949.txt" pathSource="C:\Users\John\Desktop\a.txt" preferredName="a.txt" />

</one:OE>

Next, I will remove all the unneeded data and look at what I will add to the page when I add a new file:

<one:OE >

<one:InsertedFile pathSource="C:\Users\John\Desktop\My New File.txt" preferredName="MY NEW FILE.txt" />

</one:OE>

The lastModifiedTime and creationTime will be added once I update the page with new content. The pathCache attribute is a bit more interesting - it will be created by OneNote when I update the page content. It points to the cached copy of the file that will be stored at the location to which the attribute points. In this example, I am using the default OneNote location, but users can override the cache path in File | Options if they want. Also notice that the file name gets changed to a GUID by OneNote. Since OneNote will generate this GUID, there is not much more to say about it.

pathSource may be a little interesting if I'm using a network path. Obviously, if the path can't be located, the file will not be added. Since this is local, I should be OK. It's an optional parameter (they all are, actually), but I need to specify it in this case so the local file will get picked up and added to OneNote.

After I add this OE below the original OE that pointed at a.txt, I can update the page content. When I look at the page, I see this:

clip_image001

And obviously the preferredName is what shows as the file name. I put it all in capital letters so it would be easier to see.

Let me know if this helps.

Questions, comments, concerns and criticisms always welcome,

John