DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Using OneNote API to Count Degustated Beers

Gunnar Peipman user avatar by
Gunnar Peipman
·
May. 11, 14 · · Interview
Like (0)
Save
Tweet
3.78K Views

Join the DZone community and get the full member experience.

Join For Free

i am heavy onenote user as onenote is easybut still powerful application to keep important information and organize it the way i like. not to mention powerful cloud back-end that makes my onenote notebooks available for me on desktop, tablet and mobile phone. in this posting i will show you some code i use to update the number of degustated beers in beer diary.

lazy as i am i needed dumb and simple solution to update the number of degustated beers in my degustation diary. i had no time enough to come out with something polished and nice. but my solution still works fine and i can run it on windows task scheduler so the fresh number of degustated beers is also available when i’m using surface rt or windows phone to add new beers to degustation section. the wonderful power of clout it is!

structure of degustation section

structure of beer degustation diary in my beers onenote i have special section for degustation. image on right shows example of the structure. here are hierarchy levels i use:

  1. country
  2. brewery
  3. beer

so, first level is always country, under countries i have breweries and under breweries the beers they brew and what i have already tried and documented.

in the beginning you don’t need automated way to count beers because it’s easy to do manually. it takes just minute. but after 80 beers you suddenly understand that there must be some automated way how to do counting.

there is one more top-level page: section entry page. first block of this page shows me the number of documented beers and this is the number i want to update automatically.

counting beers

as beers are always on third level in page hierarchy and always in same notebook and same section it’s easy to do some hardcoding that for nice solutions i try to avoid.

here’s what my code does:

  1. find beer degustation section, section index and beer pages
  2. take first block of index page
  3. write number of beers to this block

and here is the code itself (ugly but works for me):

static void main(string[] args)
{
    string strnamespace = "http://schemas.microsoft.com/office/onenote/2013/onenote";
    var app = new application();
    var outputxml = "";
    var pagexml = "";
 
    // load whole notebook sections hierarchy
    app.gethierarchy(null, hierarchyscope.hssections, out outputxml);
 
    xmldocument xmldoc = new xmldocument();
    xmldoc.loadxml(outputxml);
    xmlnamespacemanager nsmgr = new xmlnamespacemanager(xmldoc.nametable);
    nsmgr.addnamespace("one", strnamespace);
 
    // find degustation section
    xmlnode xmlnode = xmldoc.selectsinglenode("//one:section[@name='õllekataloog']", nsmgr);
    var id = xmlnode.attributes["id"].value;
    app.gethierarchy(id, hierarchyscope.hspages, out outputxml); //pagelevel="3"
    xmldoc.loadxml(outputxml);
 
    // find beer pages
    var nodes = xmldoc.selectnodes("//one:page[@pagelevel=3]", nsmgr);
 
    // find degustation section index page
    xmlnode = xmldoc.selectsinglenode("//one:page[@name='õlleindeks']", nsmgr);
    app.getpagecontent(xmlnode.attributes["id"].value, out outputxml, pageinfo.pibasic);
    var pageid = xmlnode.attributes["id"].value;
 
    // load first block from index page
    xmldoc.loadxml(outputxml);
    xmlnode = xmldoc.selectsinglenode("//one:page/one:outline/one:oechildren/one:oe/one:t", nsmgr);
    pagexml = "";
 
    // add total number of beers to index page
    xmlnode.innertext = "total: " + nodes.count + "\r\n";
    pagexml = xmldoc.outerxml;
 
    // save index page
    app.updatepagecontent(pagexml);
}

nb! don’t forget to add reference to onenot type library if you want to run or build this code in your machine. it is expected that onenote 2013 is installed on your machine!

degustation section index page image on left shows simple example of beer index page. instead of going manually through all subpages in degustation section and making mistakes on counting them i have now nice automated way how to update the number of degustated beers.

conclusion

it’s not hard to use onenote api to modify contents of notebooks. at first the api is cumbersome and seems kinky but after some messing with it and onenote xml it is easy to do whatever you like to do with your notebooks. one thing to notice – it is possible to build onenote add-ons too but it takes more effort and some hacking on visual studio to make add-ons work.

related posts

  • performance of fibonacci numbers algorithms
  • reading embedded files at runtime
  • windows phone 7 development: reading rss feeds
  • code contracts and inheritance
  • foreach method and blonde me

the post using onenote api to count degustated beers appeared first on gunnar peipman - programming blog .

API

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • OLAs vs. SLAs vs. UCs: What They Mean and How They're Different
  • OPC-UA, MQTT, and Apache Kafka: The Trinity of Data Streaming in IoT
  • Top Six Kubernetes Best Practices for Fleet Management
  • Stupid Things Orgs Do That Kill Productivity w/ Netflix, FloSports & Refactoring.club

Comments

Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo