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.