Sample Code/App :: OneNote Stats
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.
Steps:
- 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
Code:
using System.Xml;
using OneNote = Microsoft.Office.Interop.OneNote;
namespace OneNoteStats
{
class Program
{static void Main(string[] args)
{//string to store all of the OneNote hierarchy XML
string onHierarchy;
//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();
xdoc.LoadXml(onHierarchy);
string OneNoteNamespace = "https://schemas.microsoft.com/office/onenote/12/2004/onenote";
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("one", OneNoteNamespace);
//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.
Comments
Anonymous
August 13, 2006
I use OneNote both at home and work. I am planning to build an app using Amazon's Online Storage service (paid subscription). I am always looking for some samples on OneNote. Would it be possible to use password protect & encrypt Notes so they are not visible if someone decides to go against the raw XML file.Anonymous
August 14, 2006
These are great questions Kris. I will address them in an upcoming blog post. Contact me if you need anything else. thanks for the commentAnonymous
August 15, 2006
PingBack from http://www.onenotepowertoys.com/2006/08/16/dan-escapas-onenote-blog/Anonymous
August 16, 2006
I would love to see more example code. Love to see some more and different examples of the UpdateHierarchy to update and create sections, pages and outlines. Right now there seems to be just one example of update.Anonymous
August 18, 2006
Great I love this feedback...I will work on some blog posts to cover this!Anonymous
June 07, 2008
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 pageAnonymous
December 07, 2015
The comment has been removed