0

Currently I am trying to add content to a OneNote 2013 page using the UpdatePageContent function from the OneNote API provided by the Microsoft.Office.Interop.OneNote reference. However, I always obtain the error code 0x80042001, which is described as The XML is invalid in the documentation. Yet, I cannot understand for what reason my XML in the following simple code would be invalid.

using System;
using OneNote = Microsoft.Office.Interop.OneNote;

namespace ConsoleApplicationOneNoteTest
{
    class Program
    {
        static void Main(string[] args)
        {
            OneNote.Application onenoteApp = new OneNote.Application();

            string pageChangesXml = "<?xml version=\"1.0\"?>" +
                "<one:Page xmlns:one=\"http://schemas.microsoft.com/office/onenote/2013/onenote\" " +
                "ID=\"{787BF30C-0B35-4D74-A13B-AD00D046F97D}{1}{E19479572117852244555720107518407865185197031}\">" +
                "<one:Outline>" +
                "<one:OEChildren>" +
                "<one:OE>" +
                "<one:T>" +
                "<![CDATA[New Text]]>" +
                "</one:T>" +
                "</one:OE>" +
                "</one:OEChildren>" +
                "</one:Outline>" +
                "</one:Page>";
           onenoteApp.UpdatePageContent(pageChangesXml, DateTime.MinValue);
        }
    }
}

The page ID was obtained from the OneNote Notebook hierarchy and I can confirm it to be correct, since calls to GetPageContent using the same ID work fine. So it appears to me that there actually must exist some problem with the XML. What am I missing out?


Edit: Following dbc's comment, I looked deeper into this answer, which originally appears to come from here, although it treats editing an existing entry in a OneNote page instead of adding new content, which is what I want to do. Reducing the suggested code to the necessary leaves me with the following:

using System;
using System.Linq;
using XML = System.Xml.Linq;
using OneNote = Microsoft.Office.Interop.OneNote;

class Program
{
    static void Main(string[] args)
    {
        OneNote.Application onenoteApp = new OneNote.Application();
        XML.XNamespace ns = GetNamespace(onenoteApp);
        string PageId = "{787BF30C-0B35-4D74-A13B-AD00D046F97D}{1}{E19479572117852244555720107518407865185197031}";
        string xml;

        onenoteApp.GetPageContent(PageId, out xml, OneNote.PageInfo.piAll);

        var doc = XML.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(), DateTime.MinValue);
    }

    static XML.XNamespace GetNamespace(OneNote.Application app)
    {
        string xml;

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

However, now I am recieving error 0x8004200B, which is explained as The section is read-only in the documentation. Of course no read-only access was ever set and manually I have no trouble editing said page. Also, Google is not really helpful on that error.

Maybe the different error code means that my XML now has a format, which is accepted by OneNote (although I cannot see a difference to my previous approach). But still I have no luck in modifying an existing page. In particular, the two linked threads suggest that above code should work out-of-the-box, since it is the accepted answer in both cases. In my scenario, on the other hand, this doesn't work at all. What is going on here?


Edit 2: My second code block works on a different machine running Win 7 x64, OneNote 2013 and Visual Studio 2015, where the machine I used so far is running Win 7 x86, OneNote 2013 and Visual Studio 2010. So maybe the VS2010 library is just to old to talk to OneNote 2013 properly? I will try to collect some more information and see if I can make an answer out of it.

ranguwud
  • 285
  • 3
  • 12
  • 2
    At a glance, your opening `Outline` element has a space in it. You should probably create your XML using an XML library like LINQ to XML rather than relying on string concatenation. – Charles Mager Jan 18 '16 at 08:14
  • The XML itself appears okay (although I'd also second Charles suggestion to use a library rather than string mangling). I'm guessing "New Text" isn't the real value, so perhaps the problem lies there? – Damien_The_Unbeliever Jan 18 '16 at 08:30
  • @CharlesMager I agree with your suggestion, but for the sake of this question it should not make any difference and it is probably even clearer to have the string written explicitly. I double checked for the additional space and it doesn't affect the problem, so I removed it from the original question. – ranguwud Jan 18 '16 at 09:14
  • @Damien_The_Unbeliever The code I am showing is the exact code I am trying to run. I also added my uses now, so the code should be stand-alone and can be executed with Visual Studio (VS2010 in my case). Of course "New Text" will be changed for something else in the future, but for the moment not even that is working as expected. – ranguwud Jan 18 '16 at 09:17
  • Even if there are no suggested solutions so far, could somebody test whether the problem exists on other machines as well? – ranguwud Jan 18 '16 at 12:04
  • Possibly related: https://stackoverflow.com/questions/27294510/how-to-write-to-a-onenote-2013-page-using-c-sharp-and-the-onenote-interop – dbc Jan 18 '16 at 16:20
  • According to the API reference, where you have `DateTime.MinValue`, you should have the actual last-modified date of the note: dateExpectedLastModified—(Optional) The date and time that you think the page you want to update 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 page was last modified. https://msdn.microsoft.com/EN-US/library/office/jj680120.aspx – Eris Jan 18 '16 at 21:32
  • @Eris Looking at other threads, like the two linked ones, it appears that `DateTime.MinValue` is just the right way to say "zero" in this place. Leaving out the argument completely still raises the same error. When passing `DateTime.Now` instead, OneNote actually complains about the wrong date, as excpected. So I do not think that this argument is the actual problem. – ranguwud Jan 18 '16 at 21:40
  • silly thought: does this page belong to a notebook you own? Or could it be from a notebook shared with you with ReadOnly access? – DipakBoyed Jan 19 '16 at 18:19
  • @DipakBoyed Thanks for the idea, but this is not the case. It's just a normal, locally stored notebook. I am going to test a bit further, whether I can for example create new pages in the section using the API. Maybe this will clarify something. – ranguwud Jan 19 '16 at 19:43

1 Answers1

1

I've had also problems working with C# and OneNote. Everytime I was calling a "write"-method, meaning that I wanted to edit something in a OneNote-File, I got the Exception of HRESULT: 0x8004200B.

I was using the Microsoft OneNote 12.0 type Library as Reference. I tried to use the 14.0 type Library as well, but on every method the complier said something like

Error 1 'Microsoft.Office.Interop.OneNote.Application' does not contain a definition for 'GetHierarchy' and no extension method 'GetHierarchy' accepting a first argument of type ...

So the solution for me was using the OneNote 14.0 type Library AND set the Embed Interop Types to FALSE. You can find it by clicking on the OneNote Reference (when you already added it) down in the proberties.

Since then, everything worked fine!

toniweser
  • 55
  • 8