Find Currently Viewed page, section and notebook
It has been awhile since I have posted anything about the OneNote API and I apologize/apologise for that; we are busy planning the next release of OneNote. In fact if I haven't mentioned it before the codename is Office14, the code names are always based on the version number of Office. So while we have a bunch of great ideas of for the next release of OneNote we are always listening for suggestions and comments. If you have thoughts please email me or use our Connect site.
That being said I wanted to answer a question that came my way a few days ago from Michael @ Mindjet (see his addin here). He wanted to know this:
How do I determine the section that is currently visible to the user.
Well here is a quick method to help you with that:
private
string findCurrentlyViewedPage()
{
string
strXML;
string
strNamespace = "https://schemas.microsoft.com/office/onenote/2007/onenote";
// Load
the xml into a document
XmlDocument
xmlDoc = new XmlDocument();
// Get
the hierarchy from the root to pages
onApp.GetHierarchy(null, OneNote.HierarchyScope.hsPages,
out strXML);
// Load
the xml into a document
xmlDoc.LoadXml(strXML);
//Create
an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager
nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("one", strNamespace);
// Find
the page ID of the active page
XmlElement
xmlActivePage = (XmlElement)xmlDoc.SelectSingleNode("//one:Page[@isCurrentlyViewed=\"true\"]",
nsmgr);
string
strActivePageID = xmlActivePage.GetAttribute("ID");
return
strActivePageID;
}
So I bet if you wanted to do the same thing for the sections you could just change the SelectSingleNode XPath query from one:Page to one:Section and it will work. Actually this is correct! And you can change your query to only be for hsSections as well.
private
string findCurrentlyViewedSection()
{
string
strXML;
string
strNamespace = "https://schemas.microsoft.com/office/onenote/2007/onenote";
// Load
the xml into a document
XmlDocument
xmlDoc = new XmlDocument();
// Get
the hierarchy from the root to pages
onApp.GetHierarchy(null, OneNote.HierarchyScope.hsSections,
out strXML);
// Load
the xml into a document
xmlDoc.LoadXml(strXML);
//Create
an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager
nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("one", strNamespace);
// Find
the page ID of the active page
XmlElement
xmlActivePage = (XmlElement)xmlDoc.SelectSingleNode("//one:Section[@isCurrentlyViewed=\"true\"]",
nsmgr);
string
strActivePageID = xmlActivePage.GetAttribute("ID");
return
strActivePageID;
}
And finally for the currently viewed notebook:
private
string findCurrentlyViewedNotebook()
{
string
strXML;
string
strNamespace = "https://schemas.microsoft.com/office/onenote/2007/onenote";
// Load
the xml into a document
XmlDocument
xmlDoc = new XmlDocument();
// Get
the hierarchy from the root to pages
onApp.GetHierarchy(null, OneNote.HierarchyScope.hsNotebooks,
out strXML);
// Load
the xml into a document
xmlDoc.LoadXml(strXML);
//Create
an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager
nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("one", strNamespace);
// Find
the page ID of the active page
XmlElement
xmlActivePage = (XmlElement)xmlDoc.SelectSingleNode("//one:Notebook[@isCurrentlyViewed=\"true\"]",
nsmgr);
string
strActivePageID = xmlActivePage.GetAttribute("ID");
return
strActivePageID;
}
So this is just something that Michael asked me and I wanted to share with all of you.
Comments
Anonymous
January 17, 2007
All the code examples I see for AddNamespace() in relation to OneNote hardcode a namespace, which to me seems like a bad idea. It locks you into the current version you're working against. Why not do this instead? nsmgr.AddNamespace("one", doc.DocumentElement.GetNamespaceOfPrefix("one")); That way you look up with the same namespace as what is already in the document, making it version free.Anonymous
March 27, 2007
Hi all! - [url=http://lineage2account.mybbland.com]lineage 2 accounts[/url] <a href="http://lineage2account.mybbland.com">lineage 2 accounts</a> http://lineage2account.mybbland.com Thanks!Anonymous
May 12, 2009
How to use FindPages method for onenote to search pages within section.