OneNote Import Example: Generating GUIDs and XML
One thing I didn’t have time to include in my OneNote SP1 Preview article entry was a simple example that generates GUIDs for elements as necessary, and then creates its own XML string for the Import method. That’s something I definitely want to include in the final MSDN article, and I thought it might be helpful to preview it here first as well. So without further ado:
The following example demonstrates how you can programmatically generate xml strings and object GUIDs for use with the OneNote import functionality. In this sample, the user specifies a location, and optionally, a title, for a new page that the application then creates using the CSimpleImporterClass.Import method.
Once the sample has stored the user input in two variables, it calls the NewGuid method to generate a new GUID for the page to be created. Note that because the NewGuid method is a shared method, you do not have to instantiate a Guid object in order to call it. In order for OneNote to correctly process it, the generated GUID must be wrapped in angle brackets ({}).
The sample then constructs the simple xml document required for the Import method. The code employs an XMLTextWriter object and its various methods to generate an xml stream. The code creates <Import> and <EnsurePage> elements and their required attributes. If the user has specified a page title, the code creates the optional title attribute of the <EnsurePage> element as well. Once it has created the xml stream, it flushes the XmlTextWriter and moves the position back to the beginning of the stream.
At this point, the code uses a StreamReader object to write the contents of the xml stream to the console, to demonstrate what the xml generated looks like. Because the Import method takes a string, not a stream, the code uses the StreamReader.ReadToEnd method to get a string representing the contents of the xml stream. The code then passes this string as the required argument to the Import method, thereby creating the desired page. Finally, the code calls the NavigateToPage method, and passes it the page path and page GUID variables set earlier, in order to navigate to the new page in OneNote.
Imports System.Xml
Imports System.IO
…
Private Sub btnCreate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCreate.Click
Dim pagePath As String
Dim pageTitle As String
Dim pageGuid As String
Dim XmlImportStream As MemoryStream = New MemoryStream
Dim XmlImportWriter As XmlTextWriter = _
New XmlTextWriter(XmlImportStream, Nothing)
Dim strEnsurePage As String
Dim OneNoteImporter As OneNote.CSimpleImporterClass
'store user input for path and optional page title
pagePath = Me.txtPagePath.Text.ToString
pageTitle = Me.txtPageTitle.Text.ToString
'generate a new GUID for the page to be created
pageGuid = "{" & Guid.NewGuid.ToString & "}"
'Generate the xml as a stream
With XmlImportWriter
.WriteStartDocument()
'Generate root Import element
'and specify OneNote schema namespace
.WriteStartElement("Import")
.WriteAttributeString("xmlns", _
"https://schemas.microsoft.com/office/onenote/01/2004/import")
.WriteStartElement("EnsurePage")
'Generate required path attribute
.WriteAttributeString("path", pagePath)
'Generate required GUID attribute
.WriteAttributeString("guid", pageGuid)
'Generate opotional title attribute, if title was specified
If Not pageTitle = "" Then
.WriteAttributeString("title", pageTitle)
End If
.WriteEndElement()
.WriteEndElement()
.WriteEndDocument()
End With
'Flush the xmltextwriter
XmlImportWriter.Flush()
'Move to the start of the xml stream
XmlImportStream.Position = 0
'Create a streamreader for the xml stream
Dim XmlImportReader As StreamReader = New StreamReader(XmlImportStream)
'Write the xml stream to the console
Console.WriteLine(XmlImportReader.ReadToEnd)
Console.ReadLine()
'Move back to the start of the xml stream
XmlImportStream.Position = 0
'Store entire xml stream in variable as a string
strEnsurePage = XmlImportReader.ReadToEnd
'Create instance of OneNote simple importer
OneNoteImporter = New OneNote.CSimpleImporterClass
'Pass xml string to import method to create page
OneNoteImporter.Import(strEnsurePage)
'Navigate to new page, based on path and GUID variables
OneNoteImporter.NavigateToPage(pagePath, pageGuid)
End Sub
For more information on creating and using GUIDs, see GUID Structure in the .NET Framework Class Library.
For more information on creating XML using the .NET framework, see Well-Formed XML Creation with the XMLTextWriter.