Table of Contents

Class OneNote

Namespace
LinqToOneNote
Assembly
LinqToOneNote.dll

A static wrapper class around the Microsoft.Office.Interop.OneNote.Application class, allowing for lazy acquirement and release of a OneNote COM object. In addition to exposing the OneNote's API

public static class OneNote
Inheritance
OneNote
Inherited Members

Remarks

A OneNote COM object is required to access any of the OneNote API.

Properties

ComObject

Use this only if you know what you are doing. The COM Object instance of the OneNote application.

public static Application ComObject { get; }

Property Value

Application
See Also

ComObjectMode

The mode defining how the OneNote COM object is managed. See ComObjectMode.

public static ComObjectMode ComObjectMode { get; }

Property Value

ComObjectMode
See Also

HasComObject

Indicates whether the class has a usable COM Object instance.

public static bool HasComObject { get; }

Property Value

bool

Remarks

When true a "Microsoft OneNote" process should be visible in the Task Manager.

See Also

Methods

CloseNotebook(Notebook, bool)

Causes OneNote to synchronizes any offline files with the notebook, if necessary, and then closes the specified notebook. After the method returns, the notebook no longer appears in the list of open notebooks in the OneNote user interface (UI).

public static void CloseNotebook(Notebook notebook, bool force = false)

Parameters

notebook Notebook

The specified OneNote notebook.

force bool

true to close the notebook, even if there are changes in the notebook that OneNote cannot sync before closing; otherwise, false.

CreateNotebook(string, OpenMode)

Creates a notebook with a title equal to name located in the default notebook location.

public static Notebook CreateNotebook(string name, OpenMode openMode = OpenMode.None)

Parameters

name string

The name of the new notebook.The directory to create the notebookWhether to open the newly created notebook in OneNote immediately.

openMode OpenMode

The name of the new notebook.The directory to create the notebookWhether to open the newly created notebook in OneNote immediately.

Returns

Notebook

The newly created notebook.

Exceptions

ArgumentException

Thrown if the name is not valid for a notebook.

See Also

CreateNotebook(string, string, OpenMode)

Creates a notebook with a title equal to name located in the directory. If directory is null, the default notebook location is used.

public static Notebook CreateNotebook(string name, string directory, OpenMode openMode = OpenMode.None)

Parameters

name string

The name of the new notebook.

directory string

The directory to create the notebook

openMode OpenMode

Whether to open the newly created notebook in OneNote immediately.

Returns

Notebook

The newly created notebook.

Exceptions

ArgumentException

Thrown if the name is not valid for a notebook.

IOException

Thrown if the specified directory is not null and does not exist.

See Also

CreatePage(Section, string, OpenMode)

Creates a page with a title equal to name located in the specified section.

public static Page CreatePage(Section section, string name = "", OpenMode openMode = OpenMode.None)

Parameters

section Section

The section to create the page in.

name string

The title of the page.

openMode OpenMode

Specifies whether/how the newly created page should be opened.

Returns

Page

The newly created page.

Exceptions

ArgumentNullException

Thrown if the parameter section is null

ArgumentException

Thrown if the section is in a recycle bin

CreateQuickNote(OpenMode)

Creates a quick note page located at the users quick/default notes location.

public static string CreateQuickNote(OpenMode openMode = OpenMode.None)

Parameters

openMode OpenMode
Specifies whether/how the newly created page should be opened.

Returns

string

The Id of the newly created quick note page.

CreateQuickNote(out Page, OpenMode)

Creates a quick note Page located at the users quick/default notes location and outputs its as the parameter page.

public static void CreateQuickNote(out Page page, OpenMode openMode = OpenMode.None)

Parameters

page Page

The newly created quick note page as a Page object.

openMode OpenMode
Specifies whether/how the newly created page should be opened.

CreateQuickNote(string, OpenMode)

Creates a quick note page with the title specified by name, located at the users quick/default notes location.

public static string CreateQuickNote(string name, OpenMode openMode = OpenMode.None)

Parameters

name string
The title of the page.
openMode OpenMode
Specifies whether/how the newly created page should be opened.

Returns

string

The Id of the newly created quick note page.

CreateQuickNote(string, out Page, OpenMode)

Creates a quick note Page with the title specified by name, located at the users quick/default notes location and outputs its as the parameter page.

public static void CreateQuickNote(string name, out Page page, OpenMode openMode = OpenMode.None)

Parameters

name string
The title of the page.
page Page

The newly created quick note page as a Page object.

openMode OpenMode
Specifies whether/how the newly created page should be opened.

CreateSectionGroup<TNotebookOrSectionGroup>(TNotebookOrSectionGroup, string, OpenMode)

Creates a section group with a title equal to name located in the specified parent.

public static SectionGroup CreateSectionGroup<TNotebookOrSectionGroup>(TNotebookOrSectionGroup parent, string name, OpenMode openMode = OpenMode.None) where TNotebookOrSectionGroup : INotebookOrSectionGroup

Parameters

parent TNotebookOrSectionGroup

The hierarchy item to create the section group in.

name string

The name of the new section group.

openMode OpenMode

Whether to open the newly created section group in OneNote immediately.

Returns

SectionGroup

The newly created section group.

Type Parameters

TNotebookOrSectionGroup

Represents a notebook or a section group.

Exceptions

ArgumentException

Thrown if the name is not a valid section group name.

See Also

CreateSection<TNotebookOrSectionGroup>(TNotebookOrSectionGroup, string, OpenMode)

Creates a section with a title equal to name located in the specified parent.

public static Section CreateSection<TNotebookOrSectionGroup>(TNotebookOrSectionGroup parent, string name, OpenMode openMode = OpenMode.None) where TNotebookOrSectionGroup : INotebookOrSectionGroup

Parameters

parent TNotebookOrSectionGroup

The hierarchy item to create the section in.

name string

The name of the new section.

openMode OpenMode

Whether to open the newly created section in OneNote immediately.

Returns

Section

The newly created section.

Type Parameters

TNotebookOrSectionGroup

Represents a notebook or a section group.

Exceptions

ArgumentException

Thrown if the name is not a valid section name.

See Also

DeleteItem(IDeletable, DateTime, bool)

Deletes the hierarchy item from the OneNote notebook hierarchy. For notebooks use CloseNotebook(Notebook, bool). Does nothing if the item is already in the Recycle Bin.

public static void DeleteItem(IDeletable item, DateTime dateExpectedLastModified = default, bool deletePermanently = false)

Parameters

item IDeletable

The item to be deleted

dateExpectedLastModified DateTime

The date and time that you think the object you want to delete was last modified. If you pass a non-zero value for this parameter, OneNote proceeds with the update only if the value you pass matches the actual date and time the object was last modified. Passing a value for this parameter helps prevent accidentally overwriting edits users made since the last time the object was modified.

deletePermanently bool

true to permanently delete the item; false to move the item into the OneNote recycle bin for the corresponding Notebook (the default). If the Notebook is in OneNote 2007 format, no recycle bin exists, so the content is permanently deleted.

FindPages(string)

Get a flattened collection of pages that match the search parameter.

public static IEnumerable<Page> FindPages(string search)

Parameters

search string

The search query. This should be exactly the same string that you would type into the search box in the OneNote UI. You can use bitwise operators, such as "AND" and "OR", which must be all uppercase.

Returns

IEnumerable<Page>

An IEnumerable<Page> that contains pages that match the search parameter.

Exceptions

ArgumentException

Thrown if search is empty or only whitespace, or if the first character of search is NOT a letter or a digit.

ArgumentNullException

Thrown if search is null.

See Also

FindPages(string, IOneNoteItem)

Get a flattened collection of pages that match the search parameter. Within the specified scope.
public static IEnumerable<Page> FindPages(string search, IOneNoteItem scope)

Parameters

search string
The search query. This should be exactly the same string that you would type into the search box in the OneNote UI. You can use bitwise operators, such as "AND" and "OR", which must be all uppercase.
scope IOneNoteItem

The hierarchy item to search within.

Returns

IEnumerable<Page>

An IEnumerable<Page> that contains pages that match the search parameter.

Exceptions

ArgumentException
ArgumentNullException

Thrown if search or scope is null.

See Also

GetBackUpLocation()

Retrieves the path on disk to the backup folder location.

public static string GetBackUpLocation()

Returns

string

The path on disk to the backup folder location.

GetDefaultNotebookLocation()

Retrieves the path on disk to the default notebook folder location, this is where new notebooks are created and saved to.

public static string GetDefaultNotebookLocation()

Returns

string

The path to the default notebook folder location.

GetDefaultNotesLocation()

Retrieves the folder path on disk to the default notes section, this is where by default quick notes are created and saved to.

public static string GetDefaultNotesLocation()

Returns

string

The folder path on disk to the default notes section.

GetFullHierarchy()

Get the full OneNote hierarchy.

public static Root GetFullHierarchy()

Returns

Root

Returns a Root object which contains the OneNote hierarchy.

GetPageContent(Page)

Gets the content of the specified page.

public static string GetPageContent(Page page)

Parameters

page Page

The page to retrieve content from.

Returns

string

An string in the OneNote XML format.

InitComObject()

Forcible initialises the static class by acquiring a OneNote COM object. Does nothing if a COM object is already accessible.

public static void InitComObject()

Exceptions

COMException

Thrown if an error occurred when trying to get the OneNote COM object.

See Also

IsValidName<THierarchyItem>(string)

Returns a value that indicates whether the supplied name is a valid for the THierarchyItem.

public static bool IsValidName<THierarchyItem>(string name) where THierarchyItem : INameInvalidCharacters

Parameters

name string

The potential new name/title of the THierarchyItem

Returns

bool

true if the specified name is not null, empty, whitespace or contains any characters from the THierarchyItem implementation of InvalidCharacters; otherwise, false.

Type Parameters

THierarchyItem

The type of hierarchy element to check the name of i.e. a Notebook, a SectionGroup or a Section

See Also

Open(INavigable, bool)

Opens the item in OneNote. If there is no OneNote window a new one is created, else whether a new window is created is defined by newWindow.

public static void Open(INavigable item, bool newWindow = false)

Parameters

item INavigable

The item to open

newWindow bool

Whether to create a new OneNote window or add to an existing one. Does nothing if there are no windows of OneNote.

Open(string, bool)

Opens the item that corresponds to the id in OneNote. If there is no OneNote window a new one is created, else whether a new window is created is defined by newWindow.

public static void Open(string id, bool newWindow = false)

Parameters

id string

The id of the item to open

newWindow bool

Whether to create a new OneNote window or add to an existing one. Does nothing if there are no windows of OneNote.

ReleaseComObject()

Releases the OneNote COM object freeing memory.

public static void ReleaseComObject()
See Also

RenameItem(IOneNoteItem, string)

Renames the specified item to newName. For notebooks, this renames its display name, not the folder on disk.

public static void RenameItem(IOneNoteItem item, string newName)

Parameters

item IOneNoteItem

The item to be renamed

newName string

The new name of the item

Exceptions

ArgumentException

Thrown if the item is in the Recycle Bin.
Thrown if newName is invalid for the item type. See IsValidName<THierarchyItem>(string).

ArgumentNullException

Thrown if item is null.

SetComObjectMode(ComObjectMode)

Sets the ComObjectMode used by the class. See ComObjectMode.

public static void SetComObjectMode(ComObjectMode mode)

Parameters

mode ComObjectMode

The mode to set.

See Also

SyncItem(INavigable)

Forces OneNote to sync the item.

public static void SyncItem(INavigable item)

Parameters

item INavigable
The item to open

UpdatePageContent(string)

Updates the content of a OneNote page with the provided xml. The chosen page depends on the ID provided in the xml. An example can be seen here at the Microsoft OneNote API documentation.

public static void UpdatePageContent(string xml)

Parameters

xml string

An string in the OneNote XML format.

Remarks

The xml must match the OneNote XML format, the schema can be found here.