I managed to create sections at very specific places within my OneNote Notebooks. Now I want to achieve the same with pages. So instead of using the unpredicteable-placing "CreateNewPage" method, I use UpdateHierarchy which works perfectly fine (for testing purpose I'm using AppendChild below).

The only issue I'm having is, that after creating the new page using UpdateHierarchy I'm completely loosing any links to the newly created page. OneNote assigns an ID and ignores all further Tags/Names I give. Also setting the One:T member used for setting the title is getting ignored - it always creates an "Untitled Page".

Am I doing anything wrong or do I need to first CreateNewPage and, using the assigned page-ID, re-place it using UpdateHierarchy?

Regards Joel

function createPage {
param([string]$title, [string]$sectionnode)

[string]$pageref=$null

# Gather the pages within the notebook
[xml]$ref = $null
$_globalOneNote["COM"].GetHierarchy($sectionnode,  [Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages, [ref]$ref)

[System.Xml.XmlNamespaceManager] $nsmgr = $ref.NameTable
$nsmgr.AddNamespace('one', "http://schemas.microsoft.com/office/onenote/2010/onenote")

# Creation of a new page
$newPage = $ref.CreateElement('one', 'Page', 'http://schemas.microsoft.com/office/onenote/2010/onenote')
$newTitle = $ref.CreateElement('one', 'Title', 'http://schemas.microsoft.com/office/onenote/2010/onenote')
$newOE = $ref.CreateElement('one', 'OE', 'http://schemas.microsoft.com/office/onenote/2010/onenote')
$newT = $ref.CreateElement('one', 'T', 'http://schemas.microsoft.com/office/onenote/2010/onenote')

$newPage.SetAttribute('name', "Olololo")
$newT.InnerText = '<![CDATA[Testtitle]]>'

$newOE.AppendChild($newT)
$newTitle.AppendChild($newOE)
$newPage.AppendChild($newTitle)

$ref.Section.AppendChild($newPage)
$_globalBJBOneNote["COM"].UpdateHierarchy($ref.OuterXML)
}
有帮助吗?

解决方案

This does the trick. Setting of title etc. must still be done using UpdatePageContent however the new page is placed properly. For putting metadata (title, indention etc.) a separate function may be used that works using the returned GUID of the createPage function.

function createPage {
param([string]$sectionnode, [string]$pagenode = $sectionnode)

# Gather the sections within the notebook
[xml]$ref = $null
$_globalBJBOneNote["COM"].GetHierarchy($sectionnode,  [Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages, [ref]$ref)

[System.Xml.XmlNamespaceManager] $nsmgr = $ref.NameTable
$nsmgr.AddNamespace('one', "http://schemas.microsoft.com/office/onenote/2010/onenote")

# Create new page
[string]$pageID = $null
$_globalBJBOneNote["COM"].createNewPage($ref.Section.ID, [ref]$pageID)

# Reload the hierarchy, now we can get the node of the new notebook
$_globalBJBOneNote["COM"].GetHierarchy($sectionnode,  [Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages, [ref]$ref)
$newPageNode = $ref.SelectSingleNode('//one:Page[@ID="' + $pageID + '"]', $nsmgr)
$referencePageNode = $ref.SelectSingleNode('//one:Page[@ID="' + $pagenode + '"]', $nsmgr)

# Reposition
[void]$ref.Section.removeChild($newPageNode)
[void]$ref.Section.InsertAfter($newPageNode, $referencePageNode)
$_globalBJBOneNote["COM"].UpdateHierarchy($ref.OuterXML)

$pageID
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top