Tuesday, August 22, 2006


A look at GetHierarchy()

GetHierarchy is the key to gaining programmatic access to your OneNote information. Prior to OneNote 12 you were limited to importing data the SimpleImport API. OneNote 2007 gives you the ability to import and export data as Xml using OneNote 2007 APIs.

The GetHierarchy method takes three parameters, the startNode Id as a string, hsScope as HierarchyScope enumeration and pbstrHierarchyXmlOut as a string to contain the Xml representation of the OneNote information.

StartNodeId parameter lists the starting node of the export. Passing in null will start the export at the top of the hierarchy. You can start the export from a lower node by passing a OneNote node id. For this post I am only going to pass in a null value for the StartNodeId parameter.

The second parameter hsScope is a enumerated type named HierarchyScope. This enumeration contains five values Children, Notebooks, Pages, Sections and Self. We will look at the difference of the exported data based on the HierarchyScope value.

The third parameter pbstrHierarchyXmlOut is an string parameter that is used to pass back the requested OneNote information as Xml. This is an out parameter and needs to be defined with the out attribute.

Instead of trying to visually explain what each HierarchyScope values do I have created a simple console application that will pass in a null for the StartNodeId and ask for a HierarchyScope. After you provide the scope the code will call GetHierachy and dump the results to the console. You will need VS to compile this simple console app. Make sure you include a reference to the OneNote interop dll. Check my previous post to see how to reference the OneNote interops.

//Start Code -------------------------------------------------------------------------------

//Make sure your project adds the OneNote API reference
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.OneNote;

namespace OneNoteDemo
class OneNote_DemoApp
ApplicationClass _AppClass;

static void Main(string[] args)
OneNote_DemoApp app = new OneNote_DemoApp();

private void Run()
//create a new OneNote ApplicationClass
_AppClass = new ApplicationClass();

private void DumpOneNoteHierarhcy()
string oneNoteAsXml;
string result;
HierarchyScope hs;
Console.WriteLine("Dump OneNote Hierarchy");
Console.WriteLine("Select a HierarchyScope:");
result = Console.ReadLine();

switch (result.ToUpper())
case "CHILDREN": { hs = HierarchyScope.hsChildren; break; }
case "NOTEBOOKS": { hs = HierarchyScope.hsNotebooks; break; }
case "PAGES": { hs = HierarchyScope.hsPages; break; }
case "SECTIONS": { hs = HierarchyScope.hsSections; break; }
case "SELF": { hs = HierarchyScope.hsSelf; break; }
default: { hs = HierarchyScope.hsSelf; break; }

Console.WriteLine("Scope = " + hs.ToString());

//The key function call is here!
_AppClass.GetHierarchy(null, hs, out oneNoteAsXml);

//ON Interops call into unmanaged code
//So make sure we destroy it
_AppClass = null;
//End Code ---------------------------------------------------------------------------------

This is a 'fun" little demo app. The xml results are dumped to the console so the reults maybe a little difficult to read. To get a better look at the results I suggest running this application from the console and piping the results to a .xml file and viewing in IE.

I created the code in C#. If there is a request for VB.Net let me know and I will see what I can do. Of course the demo code should not be interpreted as well designed and robust code. It’s demo code.

Cool to see you blogging about OneNote's API! If you have suggestions for content please let me know. I am looking to get feedback as much as possible, please just contact me via my blog which will email me (I couldn't find a contact button on this blog). Take care
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?

website statistics