Using GetSpecialLocation with the OneNote API

I just got the following question from someone and I thought I would respond with a blog post. Actually I had this _exact_ same question just a few weeks ago; here is the question:

Using OneNote Interop I use GetSpecialLocation with the SpecialLocation.slUnfiledNotesSection parameter and receive what appears to be the full path for the Unfiled Notes section. I then attempt to create a new page using the value I received from GetSpecialLocation. I receive a 0x80042004 which appears to be section does not exist. Yet the .one file is at the location returned by the GetSpecialLocation call.

This is a great question and I would like to explain things:

  • The GetSpecialLocation allows you to find the items that are open in OneNote but might not appear in the OneNote hierarchy because these items (Unfiled Notes, backup folder & default notebook location) can all appear outside of your notebooks.
  • GetSpecialLocation always returns a path which you cannot use with the other OneNote API calls, since the other calls always use the OneNote GUIDs (unique IDs).

Instead you will want to do the following:

  • GetSpecialLocation to get the string
  • Call OneNote to open the location and you will then have the ID

The code looks like this:

OneNote.Application onApp = new OneNote.Application();
string unfiledSectionPath;
onApp.GetSpecialLocation(OneNote.SpecialLocation.slUnfiledNotesSection, out unfiledSectionPath);
string unfiledSectionID;
onApp.OpenHierarchy(unfiledSectionPath, null, out unfiledSectionID, OneNote.CreateFileType.cftNone);

Now you have section ID in the variable unfiledSectionID. You can then use that to call CreateNewPage or use it in the XML that you create yourself. If you have questions please post in the comments or contact me.