Updating a broken OneNote notebook that has the wrong content type

Updating a broken OneNote notebook that has the wrong content type

Migrating content from an onsite storage server to Microsoft 365 is “relatively” straightforward from a technical perspective. However, a common issue can arise around using OneNote notebooks created locally through the desktop application before the introduction of OneDrive and SharePoint. I won’t be covering how you migrate the content in this post, as there are many resources online for this! I shall show you what happens when you migrate OneNote notebooks and how to remediate them.

A file without the correct content type

OneNote is inherently more complicated than you think regarding the file(s). OneNote notebooks are not just one file; they are a collection of files, indexes, and any embedded content you insert. This structure facilitates the complex nature of it being synced to SharePoint, for example - otherwise, it would be treated as one big binary file and fall into the same nightmare it is to migrate .pst files (please don’t).

The problem is that when you copy them into a cloud environment from someone's C drive, you will copy a folder structure rather than a single openable notebook. It will look something like this to the end user.

A broken OneNote notebook in SharePoint Online. 

If the end user explores these files and sees why their notebook isn’t opening, they will be greeted by this beautiful error message.

You can update these manually via the desktop app, but if you are dealing with scale, this isn’t always the right option for a successful change.

Updating the files content type

There are a few different ways to fix this problem, but my favourite is PowerShell, of course…

Once the folder structure is within SharePoint or OneDrive, you must find the highest level. In short, Microsoft does provide some guidance on this algorithm to identify the parent folder of a OneNote notebook, but as long as you can see the first .onetoc2 file and there aren’t any in higher levels, this is the correct folder.

What you then do to this top-level folder is tell SharePoint to update the content type to the OneNote notebook type. This is OneNote.Notebook.

A script to do it quickly

The script below shows you the basics of updating the content type for an item within a list in SharePoint. The script is only an example of how you could update each file on a one-off basis, but it would make sense to build some logic to find them all for you!

$site = "https://[tenant].sharepoint.com/sites/[site_url]"
$list = "Shared Documents"
$itemId = 2
 
Connect-PnPOnline -Url $site -Interactive -ErrorAction Stop
Set-PnPListItem -list "$($list)" -identity $($itemId) -values @{"HTML_x0020_File_x0020_Type"="OneNote.Notebook"}