How to Write to a Onenote 2013 Page Using C# and the Onenote Interop

How To Write To A OneNote 2013 Page Using C# and The OneNote Interop

I asked the same question on MSDN forums and was given this great answer. Below is a nice, clean example of how to write to OneNote using C# and the OneNote interop. I hope that this can help people in the future.

    static Application onenoteApp = new Application();
static XNamespace ns = null;

static void Main(string[] args)
{
GetNamespace();
string notebookId = GetObjectId(null, OneNote.HierarchyScope.hsNotebooks, "MyNotebook");
string sectionId = GetObjectId(notebookId, OneNote.HierarchyScope.hsSections, "Sample_Section");
string firstPageId = GetObjectId(sectionId, OneNote.HierarchyScope.hsPages, "MyPage");
GetPageContent(firstPageId);
Console.Read();
}
static void GetNamespace()
{
string xml;

onenoteApp.GetHierarchy(null, OneNote.HierarchyScope.hsNotebooks, out xml);
var doc = XDocument.Parse(xml);
ns = doc.Root.Name.Namespace;
}

static string GetObjectId(string parentId, OneNote.HierarchyScope scope, string objectName)
{
string xml;
onenoteApp.GetHierarchy(parentId, scope, out xml);

var doc = XDocument.Parse(xml);
var nodeName = "";

switch (scope)
{
case (OneNote.HierarchyScope.hsNotebooks): nodeName = "Notebook"; break;
case (OneNote.HierarchyScope.hsPages): nodeName = "Page"; break;
case (OneNote.HierarchyScope.hsSections): nodeName = "Section"; break;
default:
return null;
}

var node = doc.Descendants(ns + nodeName).Where(n => n.Attribute("name").Value == objectName).FirstOrDefault();

return node.Attribute("ID").Value;
}
static string GetPageContent(string pageId)
{
string xml;
onenoteApp.GetPageContent(pageId, out xml, OneNote.PageInfo.piAll);
var doc = XDocument.Parse(xml);
var outLine = doc.Descendants(ns + "Outline").First();
var content = outLine.Descendants(ns + "T").First();
string contentVal = content.Value;
content.Value = "modified";
onenoteApp.UpdatePageContent(doc.ToString());
return null;
}

How to create a new notebook or section using C# OneNote 2013 15.0 COM Type Library?

I guess that my approach to the problem was incorrect. I thought that if I edited the xml data to include a new Notebook or Section, that the COM library would be smart enough to detect the newly added element(s). After further reading, the correct way to create a new Notebook or Section is to use the OpenHierarchy() method.

Here is a working copy of me creating a new Section to an existing Notebook. I have not tried creating a new Notebook yet, but I assume the methodology is similar.

private void DoOpenHierarchy(Microsoft.Office.Interop.OneNote.HierarchyScope scope){    Output = "Open Hierarchy Section...\r\n";    var strXml = string.Empty;    var objectId = string.Empty;    _app.GetHierarchy(null, scope, out strXml);    try    {        var xdoc = XDocument.Parse(strXml);        var ns = xdoc.Root.Name.Namespace;        if (scope == Microsoft.Office.Interop.OneNote.HierarchyScope.hsSections)        {            var noteBook = xdoc.Root.Descendants(ns + "Notebook").FirstOrDefault();            if (noteBook != null)            {                var sectionName = "My New Section";                Output += string.Format("Attempting to create section '{0}' in {1}...\r\n", sectionName, noteBook.Attribute("name").Value);                var location = string.Format("{0}\\{1}.one", noteBook.Attribute("path").Value, sectionName);                _app.OpenHierarchy(location, string.Empty, out objectId, Microsoft.Office.Interop.OneNote.CreateFileType.cftSection);                Output += string.Format("Section ID Created: {0}\r\n", objectId.ToString());            }            else            {                Output += "ERROR: Not able to determine a 'path' in order to store new section.\r\n";            }        }    }    catch (Exception ex)    {        Output += string.Format("{0}:{1}\r\n", ex.GetType(), ex.Message);    }    Output += "\r\nOpen Hierarchy Section Done.\r\n";}

Parse Microsoft Word Text Add it To OneNote File

Your code related to reading the content of Word documents may be interesting, but unless you have some problem with that code it is entirely irrelevant to your problem of how to access/update a OneNote notebook.

For that, it sounds like what you need is an API for accessing/updating OneNote and, fortunately, the search term OneNote API is recognised by popular search engines such as Google. ;)

In case you are having trouble accessing Google for some reason, this link will take you directly to the Microsoft OneNote Developer Center

If you subsequently have trouble achieving what you need using the OneNote API itself you could then post a new, specific question about that problem, if/when it arises.



Related Topics



Leave a reply



Submit