Skip to content

Odotocodot/Linq2OneNote

Repository files navigation

LINQ to OneNote

A helper library for dealing with the OneNote Interop API (package).
Originally made for Flow.Launcher.Plugin.OneNote.

Installation

  1. Install either "OneNote" or "OneNote for Windows 10"
  2. Get the library from NuGet here

Important

This library only works for local versions of OneNote, and does not make use of the Microsoft Graph API.

Usage

Visit the API Reference to see the full API, or visit the Flow Launcher plugin to see it in action.
To see an outline of the library view the class diagram.

Getting Started

The main entry point of the library is the static class OneNoteApplication which has a collection of methods that interact with your OneNote installation.

Below is quick example on using the library to search your OneNote pages.

//Search pages that have "hello there" in the title or content.
IEnumerable<OneNotePage> pages = OneNoteApplication.FindPages("hello there");

OneNotePage page = pages.FirstOrDefault();

Console.WriteLine(page.Name);

page.OpenInOneNote();

Most functions return an IEnumerable allowing for easy use with LINQ.

Memory Management

A COM object is required to use the OneNote Interop API, by default this is acquired lazily, i.e. the first time you call a method that requires a COM object, the library gets one.

However, acquiring a COM object is slow and once retrieved, it is visible in the Task Manager (screenshot).

If you want to choose when this operation occurs, you can call OneNoteApplication.InitComObject() to forcible acquire the COM object (it does nothing if one has already been attained).

To free up the memory that the COM object takes up, rather than wait for your application to exit you can call OneNoteApplication.ReleaseComObject().

See below for an example.

//Get the COM object
OneNoteApplication.InitComObject();

//Do stuff e.g.
OneNoteNotebook notebooks = OneNoteApplication.GetNotebooks();

foreach (var notebook in notebooks)
{
    Console.WriteLine(notebook.Name)
}

IEnumerable<OneNotePage> pages = notebooks.Traverse(n => n.Children.Count() > 3).GetPages();

foreach (var page in pages)
{
    Console.WriteLine(page.Section.Name);
}

//Release the COM object to free memory
OneNoteApplication.ReleaseComObject()

Inspired By