OneNote 2007 – Class Library (.dll)
First off, let me say that this is the first .NET / C# coding I’ve attempted to do. So if you see where I’ve done something poorly or have any constructive feedback relating to the code, I’m all ears.
Secondly, I thought it would be helpful to enumerate a few resources that I’ve found useful.
- The Unknown OneNote Guy’s Blog
- Daniel Escapa
- Office 2007 (including OneNote 2007) XML schema
- What’s New for Developers in OneNote 2007 (Part 1 of 2)
- What’s New for Developers in OneNote 2007 (Part 2 of 2)
- W3 Schools XML Tutorial
Okay so let’s dive in. First off when creating your project, make sure you add a reference to the OneNote API. The Unknown OneNote Guys has a nice post on how to do this.
The first thing I did was design a class library with objects and methods for working with the OneNote API. This compiles into a DLL file, and it is referenced by my OneNote programs. I figured this would be the easiest way to ensure code re-use across the various applications I planned to develop.
In order to use the routines in the JPHOneNoteUtilities.dll, you need to add a reference to the dll in your project. Then add the line ‘using com.jamiehill;’ under the other ‘using’ statements.
You can view and download the source code for JPHOneNoteUtilities.cs here. It consists of one Class JPHOneNoteUtilies and two Structs: OneNoteXMLObject and XY.
The OneNoteXMLObject struct provides storage and retrieval for the interesting XML attributes of a OneNote XML object (this can be a Notebook, Section, Page, etc.). Whenever I retrieve something from the OneNote XML schema, I usually put it into this struct for later reference.
The XY struct is used to store dimensional or point position information about objects that I am placing into a OneNote page.
The JPHOneNoteUtilities class has a handful of methods primarily geared towards interacting with the OneNote API.
- JPHOneNoteUtilties() – Constructor.
- GetOneNoteNameSpace() – retrieves the OneNote XML namespace.
- GetOneNoteXMLAttributes(XmlNode) – given an XML node, it will retrieve all of the interesting attributes and store them in a OneNoteXMLObject structure.
- GetNodeSet(string) – given a short node name will return the XML node name (Example: Notebook -> one:Notebook).
- CreatePage(ref OneNoteXMLObject) – given a OneNoteXMLObject as a reference it will create a new OneNote page and popular the OneNoteXMLObject with the page information.
- UpdatePage(string) – given a string containing properly formated XML it will update a OneNote page. All of the data that the OneNote API needs in order to know which page to update is contained in the XML.
- DeletePage(ref OneNoteXMLObject) – given a OneNoteXMLObject it will delete the specified page.
- GetOneNoteXMLObjectByPage(string, ref string) – given an XML namespace, and a search string this routine will find the requested information and populate and return a OneNoteXMLObject.
- GetPageContent(string, PageInfo) – given a page id, and PageInfo (which is just an enumeration of return types) the routine will return a string containing all of the XML for the specified page.
- GetPageContent(string) – See above. Assumes PageInfo type of piBinaryData.
- GetOneNoteXMLObjectById(string, ref string) – given an XML namespace, and an id string this routine will find the requested information and populate and return a OneNoteXMLObject.
- GetUnfiledNotesSection() – returns a OneNoteXMLObject with the attributes for the Unfiled Notes Section.
- GetDefaultNotebookPath() – returns a string containing the path to the default notebook.
- GetNotebooks(out string) – populates the passed in string with the open Notebook’s XML.
- GetSections(out string) – populates the passed in string with the Section XML.
- GetPages(out string) – populates the passed in string with the Page XML.
- LoadXmlDoc(ref string, out XmlDocument, out XMLNamespaceManager) – Creates a new XmlDocument, loads it with the passed in string, and configures the XML namespace.
- GetNotebookNames(out string) – returns a list of the open notebooks.
There are a few more routines, I have commented out in the code, that make up a recursive decent parser for the OneNote XML structure. I had originally intended on developing an XML browser to get my feet wet (this was inspired by the Unknown OneNote Guy). However, I never got around to building a data structure to hold the information. The idea was to build something that would easily map onto a System.Windows.Forms.TreeNode; maybe one of these days I’ll get around to actually implementing this.
The other thing that I thought would be useful for this post is some examples of the XML markup that goes into creating a OneNote page.
The first example contains various elements that might be found in a page.
- a container
- bullet points
- bolded text
- text of a different font and size
- italiced text
- underlined text
- strikethrough text
- a table
- a note flag
The second example contains an embedded image.
The third example contains ink.
Check back tomorrow and I will talk about a general purpose importer, which among other things allows me to easily import email messages from PINE.
This software is distributed on an “AS IS” basis, without warranties or conditions of any kind, either express or implied.