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

  • Question

  • Hello,

    I have seen many articles about this but all of them are either incomplete or do not answer my question.  Using C# and the OneNote Interop I would like to simply write text to an existing OneNote 2013 Page.  Currently I have a OneNote Notebook, with a Section titled "Sample_Section" and a Page called "MyPage".  I need to be able to use C# code to write text to this Page, but I cannot figure out how or find any resources to do so.  I have looked at all of the code examples on the web and none answer this simple question or are able to do this.  Also many of the code examples are outdated and break when attempting to run them in Visual Studio.  I used the Microsoft code sample that shows how to change the name of a Section but I cannot find any code to write text to a Page.  There is not a simple way to do this that I can see.  I have taken a lot of time to research this and view the different examples online but none are able to help.  I vaguely understand how the OneNote Interop works through XML but any extra help understanding that would also be appreciated.  Most importantly I would really appreciate a code example that demonstrates how to write text to a OneNote 2013 Notebook Page.

    Thursday, December 4, 2014 12:41 AM

Answers

  •  Hi,

    To update the page content, we need to use UpdatePageContent Method, we could get the old page data, then update the content.

    A simple sample:

    static void Main(string[] args)
            {
                GetNamespace();
                string notebookId = GetObjectId(null, OneNote.HierarchyScope.hsNotebooks, "MyOneNote");
                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;
            }
    

    There is an article that can help you:

    # Application Interface

    http://msdn.microsoft.com/en-us/library/gg649853.aspx

    Best Regards

    Starain



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by breynolds0120 Saturday, December 6, 2014 3:02 AM
    Friday, December 5, 2014 8:10 AM
    Moderator

All replies

  •  Hi,

    To update the page content, we need to use UpdatePageContent Method, we could get the old page data, then update the content.

    A simple sample:

    static void Main(string[] args)
            {
                GetNamespace();
                string notebookId = GetObjectId(null, OneNote.HierarchyScope.hsNotebooks, "MyOneNote");
                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;
            }
    

    There is an article that can help you:

    # Application Interface

    http://msdn.microsoft.com/en-us/library/gg649853.aspx

    Best Regards

    Starain



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by breynolds0120 Saturday, December 6, 2014 3:02 AM
    Friday, December 5, 2014 8:10 AM
    Moderator
  • Exactly correct!  Thank you so much for your help
    Saturday, December 6, 2014 3:03 AM
  • Do you have any way that you can determine where on the page that the text goes, as opposed to writing to the middle of the page?
    Saturday, December 6, 2014 4:02 AM
  • Hi,

    I suggest that you could create a new thread for this issue.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 8, 2014 1:33 AM
    Moderator