The following (see the full code below) successfully adds a page in a section in Onenote. What I don't seem to be able to do is to get the lastly created page on top of the section. Here's the code that is puzzling me.

 onenoteApp.GetHierarchy(sectionId, OneNote.HierarchyScope.hsPages, out string xmlPages);
 var docPages = XDocument.Parse(xmlPages);
 var lastNode = docPages.LastNode;
 lastNode.Remove();
 docPages.AddFirst(lastNode);

GetHIerachy() just loads xml into xmlPages.

The problem is stricly XML, I don't think OneNote is a problem (at this stage), as docPage.document (the xml string) is in the same state at the end as when docPages was Parsed.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using OneNote = Microsoft.Office.Interop.OneNote;

namespace CreateOneNotePage
{
    class Program
    {
        static OneNote.Application onenoteApp = new OneNote.Application();
        static XNamespace ns = null;
        static void Main(string[] args)
        {
            onenoteApp.GetHierarchy(null, OneNote.HierarchyScope.hsNotebooks, out string xml);
            GetNamespace();
            string notebookId = GetObjectId(null, OneNote.HierarchyScope.hsNotebooks, "CreateOnenotePage");
            string sectionId = GetObjectId(notebookId, OneNote.HierarchyScope.hsSections, "Projects");
            string pageId = CreatePage(sectionId, "TEst" + DateTime.Now.ToString("hhmmss"));
            Console.WriteLine(pageId);
            Console.ReadKey();
        }

        private 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 CreatePage(string sectionId,string pageName)
        {
            onenoteApp.CreateNewPage(sectionId, out string pageId);
            onenoteApp.GetPageContent(pageId, out string xml, OneNote.PageInfo.piAll);
            var doc = XDocument.Parse(xml);
            var title = doc.Descendants(ns + "T").First();
            title.Value = pageName;
            onenoteApp.UpdatePageContent(doc.ToString());
            onenoteApp.GetHierarchy(sectionId, OneNote.HierarchyScope.hsPages, out string xmlPages);
            var docPages = XDocument.Parse(xmlPages);
            var lastNode = docPages.LastNode;
            lastNode.Remove();
            docPages.AddFirst(lastNode);
            onenoteApp.UpdateHierarchy(docPages.ToString());
            return pageId;
        }
    }
}

Recommended Answers

I worry about deleting a thing before I move a thing. Since your oject lastNode is removed in line 4, I worry what line 5 would do.
Also check out the answer at

Jump to Post

All 4 Replies

Re: xml - how to change the sequence of nodes. 80 80

I worry about deleting a thing before I move a thing. Since your oject lastNode is removed in line 4, I worry what line 5 would do.
Also check out the answer at https://stackoverflow.com/questions/2437158/whats-the-best-way-to-move-a-child-up-in-a-net-xmldocument where an insert method is used and remove is not used at all.

Re: xml - how to change the sequence of nodes. 80 80

Thank you very much. My problem inow s that docPages is a XNode and not an XmlNode. Only the latter seems to have a InserBefore method.

Would you have an idea how I should change my code so that I get XmlNodes?

Re: xml - how to change the sequence of nodes. 80 80

Just a thought. Microsoft has their API all over the map but looking at https://msdn.microsoft.com/en-us/library/system.xml.linq.xnode_methods(v=vs.110).aspx

I see the XNode.AddBeforeSelf Method (Object) method which looks interesting. That is, since no insert, maybe this to the first object?

Re: xml - how to change the sequence of nodes. 80 80

You were right. It works now. Well OneNote does not seem not very happy, but that's another problem.
Many thanks for your help.

       static string CreatePage(string sectionId,string pageName)
        {
            onenoteApp.CreateNewPage(sectionId, out string pageId);
            onenoteApp.GetPageContent(pageId, out string xml, OneNote.PageInfo.piAll);
            var doc = XDocument.Parse(xml);
            var title = doc.Descendants(ns + "T").First();
            title.Value = pageName;
            onenoteApp.UpdatePageContent(doc.ToString());
            onenoteApp.GetHierarchy(sectionId, hsScope: OneNote.HierarchyScope.hsPages, pbstrHierarchyXmlOut: out string xmlPages);
            var xDoc = XDocument.Parse(xmlPages);
            var xPages = xDoc.Descendants(ns + "Page");
            XElement xFirstPage = xPages.FirstOrDefault();
            XElement xLastPage = xPages.Where(x => x.Attribute("ID").Value == pageId).FirstOrDefault();
            xFirstPage.AddBeforeSelf(xLastPage);
            xLastPage.Remove();
            onenoteApp.UpdateHierarchy(xDoc.ToString());
            return pageId;
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.