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
- See Also
HasComObject
Indicates whether the class has a usable COM Object instance.
public static bool HasComObject { get; }
Property Value
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
notebookNotebookThe specified OneNote notebook.
forcebooltrue 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
namestringThe name of the new notebook.The directory to create the notebookWhether to open the newly created notebook in OneNote immediately.
openModeOpenModeThe name of the new notebook.The directory to create the notebookWhether to open the newly created notebook in OneNote immediately.
Returns
Exceptions
- ArgumentException
Thrown if the
nameis 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
namestringThe name of the new notebook.
directorystringThe directory to create the notebook
openModeOpenModeWhether to open the newly created notebook in OneNote immediately.
Returns
Exceptions
- ArgumentException
Thrown if the
nameis not valid for a notebook.- IOException
Thrown if the specified
directoryis 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
sectionSectionThe section to create the page in.
namestringThe title of the page.
openModeOpenModeSpecifies whether/how the newly created page should be opened.
Returns
Exceptions
- ArgumentNullException
Thrown if the parameter
sectionis null- ArgumentException
Thrown if the
sectionis 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
openModeOpenMode- Specifies whether/how the newly created page should be opened.
Returns
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
pagePageThe newly created quick note page as a Page object.
openModeOpenMode- 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
namestring- The title of the page.
openModeOpenMode- Specifies whether/how the newly created page should be opened.
Returns
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
namestring- The title of the page.
pagePageThe newly created quick note page as a Page object.
openModeOpenMode- 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
parentTNotebookOrSectionGroupThe hierarchy item to create the section group in.
namestringThe name of the new section group.
openModeOpenModeWhether to open the newly created section group in OneNote immediately.
Returns
- SectionGroup
The newly created section group.
Type Parameters
TNotebookOrSectionGroupRepresents a notebook or a section group.
Exceptions
- ArgumentException
Thrown if the
nameis 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
parentTNotebookOrSectionGroupThe hierarchy item to create the section in.
namestringThe name of the new section.
openModeOpenModeWhether to open the newly created section in OneNote immediately.
Returns
Type Parameters
TNotebookOrSectionGroupRepresents a notebook or a section group.
Exceptions
- ArgumentException
Thrown if the
nameis 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
itemIDeletableThe item to be deleted
dateExpectedLastModifiedDateTimeThe 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.
deletePermanentlybooltrue 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
searchstringThe 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
searchparameter.
Exceptions
- ArgumentException
Thrown if
searchis empty or only whitespace, or if the first character ofsearchis NOT a letter or a digit.- ArgumentNullException
Thrown if
searchis null.
- See Also
FindPages(string, IOneNoteItem)
search parameter.
scope.
public static IEnumerable<Page> FindPages(string search, IOneNoteItem scope)
Parameters
searchstring- 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.
scopeIOneNoteItemThe hierarchy item to search within.
Returns
- IEnumerable<Page>
An IEnumerable<Page> that contains pages that match the searchparameter.
Exceptions
- ArgumentException
- ArgumentNullException
Thrown if
searchorscopeis 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
GetPageContent(Page)
Gets the content of the specified page.
public static string GetPageContent(Page page)
Parameters
pagePageThe page to retrieve content from.
Returns
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
namestringThe potential new name/title of the
THierarchyItem
Returns
- bool
true if the specified
nameis not null, empty, whitespace or contains any characters from theTHierarchyItemimplementation of InvalidCharacters; otherwise, false.
Type Parameters
THierarchyItemThe 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
itemINavigableThe item to open
newWindowboolWhether 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
idstringThe id of the item to open
newWindowboolWhether 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
itemIOneNoteItemThe item to be renamed
newNamestringThe new name of the item
Exceptions
- ArgumentException
Thrown if the
itemis in the Recycle Bin.
Thrown ifnewNameis invalid for theitemtype. See IsValidName<THierarchyItem>(string).- ArgumentNullException
Thrown if
itemis null.
SetComObjectMode(ComObjectMode)
Sets the ComObjectMode used by the class. See ComObjectMode.
public static void SetComObjectMode(ComObjectMode mode)
Parameters
modeComObjectModeThe mode to set.
- See Also
SyncItem(INavigable)
Forces OneNote to sync the item.
public static void SyncItem(INavigable item)
Parameters
itemINavigable- 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
Remarks
The xml must match the OneNote XML format, the schema can be
found here.