Last Friday I was thinking about writing a small application that went out and told me how many notebooks, sections and pages that I had open in OneNote. I was thinking about this because I was looking at the new search indexer and seeing how many pages it had indexed from my notebooks. Search has been getting really good in OneNote and I am very excited about the new release which will much improved.
Additionally I have been hearing from more people that they wanted more sample code to see how to program with the OneNote API. Since our app is still in beta I am still seeking feedback on what documentation you want. Please let me know what you are looking for and we can go from there.
Without further ado: OneNoteStats an easy application that tells you how many items open you have in OneNote.
- Create a new console project
- On the Solution Explorer under References right-click, choose Add and select the Microsoft.Office.Interop.OneNote item
- Copy and paste the code below
- Compile and run
using OneNote = Microsoft.Office.Interop.OneNote;
static void Main(string args)
//string to store all of the OneNote hierarchy XML
//bind to OneNote via the COM Interop
OneNote.Application onApp = new Microsoft.Office.Interop.OneNote.Application();
//get the OneNote hierarchy
//GetHierarchy(start (null for root), scope of what you want, were to put the output
onApp.GetHierarchy(null, Microsoft.Office.Interop.OneNote.HierarchyScope.hsPages, out onHierarchy);
//Create an XML Document, load the XML and add the OneNote namespace
XmlDocument xdoc = new XmlDocument();
string OneNoteNamespace = “http://schemas.microsoft.com/office/onenote/12/2004/onenote”;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
//Use the SelectNodes method to pass an XPath query and select the matching nodes.
//One for each top-level item
XmlNodeList notebooks = xdoc.SelectNodes(“//one:Notebook”, nsmgr);
System.Console.WriteLine(“You have “ + notebooks.Count + ” notebooks”);
XmlNodeList sections = xdoc.SelectNodes(“//one:Section”, nsmgr);
System.Console.WriteLine(“You have “ + sections.Count + ” sections”);
XmlNodeList pages = xdoc.SelectNodes(“//one:Page”, nsmgr);
System.Console.WriteLine(“You have “ + pages.Count + ” pages”);
If you have any questions…this is a really simple application and I hope to have more in the near future to show how you can work with the API.