Table of Contents

Class Extensions

Namespace
LinqToOneNote
Assembly
LinqToOneNote.dll

A static class containing extension methods for the IOneNoteItem object.

public static class Extensions
Inheritance
Extensions
Inherited Members

Fields

DefaultRelativePathSeparator

The default separator used in GetRelativePath(IOneNoteItem, bool, string).

public const string DefaultRelativePathSeparator = "\\"

Field Value

string

Methods

Close(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 Close(this 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(Root, 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(this Root root, string name, OpenMode openMode = OpenMode.None)

Parameters

root Root
name string

The name of the new notebook.

openMode OpenMode

Whether to open the newly created notebook in OneNote immediately.

Returns

Notebook

The newly created notebook.

Remarks

This method also adds the created notebook to the Notebooks collection.

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
IsValidName<THierarchyItem>(string)

CreateNotebook(Root, 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(this Root root, string name, string directory = null, OpenMode openMode = OpenMode.None)

Parameters

root Root
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.

Remarks

This method also adds the created notebook to the Notebooks collection.

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
IsValidName<THierarchyItem>(string)

CreatePage(Section, string, OpenMode)

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

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

Parameters

parent Section
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

CreateSection(INotebookOrSectionGroup, string, OpenMode)

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

public static Section CreateSection(this INotebookOrSectionGroup parent, string name, OpenMode openMode = OpenMode.None)

Parameters

parent INotebookOrSectionGroup

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.

Exceptions

ArgumentException

Thrown if the name is not a valid section name.

See Also
IsValidName<THierarchyItem>(string)

CreateSectionGroup(INotebookOrSectionGroup, string, OpenMode)

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

public static SectionGroup CreateSectionGroup(this INotebookOrSectionGroup parent, string name, OpenMode openMode = OpenMode.None)

Parameters

parent INotebookOrSectionGroup

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.

Exceptions

ArgumentException

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

See Also
IsValidName<THierarchyItem>(string)

Delete(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 Delete(this 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.

DeleteOrClose(IOneNoteItem, DateTime, bool, bool)

Extension that method that combines DeleteItem(IDeletable, DateTime, bool) and CloseNotebook(Notebook, bool), i.e. deletes the item if it is a IDeletable or closes it if it is a notebook.

public static void DeleteOrClose(this IOneNoteItem item, DateTime dateExpectedLastModified = default, bool deletePermanently = false, bool force = false)

Parameters

item IOneNoteItem

The item to be deleted or closed if it is a notebook

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.
force bool
true to close the notebook, even if there are changes in the notebook that OneNote cannot sync before closing; otherwise, false.

Exceptions

ArgumentException
See Also

GetPageContent(Page)

Gets the content of the specified page.

public static string GetPageContent(this Page page)

Parameters

page Page

The page to retrieve content from.

Returns

string

An string in the OneNote XML format.

GetRecycleBin(Notebook, out SectionGroup)

Get the recycle bin section group for the specified notebook if it exists.

public static bool GetRecycleBin(this Notebook notebook, out SectionGroup sectionGroup)

Parameters

notebook Notebook

The notebook to get the recycle bin of.

sectionGroup SectionGroup

When this method returns, sectionGroup contains the recycle bin of the notebook if it was found; otherwise, null.

Returns

bool

true if the notebook contains a recycle bin; otherwise, false.

GetRelativePath(IOneNoteItem, bool, string)

Returns the relative path of the specified item within its Notebook.

public static string GetRelativePath(this IOneNoteItem item, bool useNotebookDisplayName = true, string separator = "\\")

Parameters

item IOneNoteItem

The item to get the relative path of.

useNotebookDisplayName bool

Whether to use the notebook's DisplayName or Name when getting the path.

separator string

The separator to use between path components.

Returns

string

IsInRecycleBin(IOneNoteItem)

Returns a value that indicates whether the item is in or is a recycle bin.

public static bool IsInRecycleBin(this IOneNoteItem item)

Parameters

item IOneNoteItem

The OneNote item to check.

Returns

bool

true if the item is in or is a recycle bin; otherwise, false.

Remarks

Checks whether the item is a recycle bin section group, a deleted page, a deleted section, or the deleted pages section within a recycle bin.

See Also

ItemEquals(IOneNoteItem, IOneNoteItem)

Checks if two IOneNoteItems represent the same item in OneNote by comparing their Id.

public static bool ItemEquals(this IOneNoteItem left, IOneNoteItem right)

Parameters

left IOneNoteItem

The first item to compare.

right IOneNoteItem

The second item to compare.

Returns

bool

true if the items represent the same item in OneNote; otherwise false.

See Also

Open(INavigable)

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(this INavigable item)

Parameters

item INavigable

The item to open

Rename(IOneNoteItem, string)

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

public static void Rename(this 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.

Sync(INavigable)

Forces OneNote to sync the item.

public static void Sync(this INavigable item)

Parameters

item INavigable
The item to open

TryGetNotebook(IOneNoteItem, out Notebook)

Attempts to get the Notebook that contains the specified item. This can be false if the item is in a open section or a its a hierarchy is only partially queried.

public static bool TryGetNotebook(this IOneNoteItem item, out Notebook notebook)

Parameters

item IOneNoteItem

The item to get the notebook of.

notebook Notebook

When this method returns, notebook contains the notebook of the item if it was found; otherwise, null.

Returns

bool

true if the item Notebook can be found; otherwise, false.