You may have noticed Microsoft Graph doesn’t have a OneNote API- so if you were to get the OneNote notebook for a SharePoint group how would you go about getting it?
I came up with a hack to get the OneNote notebook(s) located in a SharePoint site. Now, when a Notebook is created it will be created in the Site Assets of your site (with some exceptions). So the first step would be to get the items from the Site Assets library.
https://{TenantURL}/sites/${SiteName/Mail}/_api/web/lists/getbytitle('Site Assets')/items
An important thing to mention is that if your site is named different than the link to your site you want to use the link name;
If I name a site ‘Spoon’ but the site location is ‘/sites/Utensils’ I would use ‘Utensils’ to locate the Site Assets items. This is true for a lot of other APIs so it’s important to remember. When in doubt the documentation will figure it out.
Now that we’re getting the items for the chosen site, we can filter these results to get the right type of item. Now, my first thought was to say that the item title ‘endsWith’ the word “Notebook” but what if it doesn’t? endsWith also is unsupported for certain parameters so we have to try something else. You have to account for regular scenarios like naming the Notebook something different or there being more than one Notebook. The only thing to do is to check for the Content Type Id. We’re checking for the type of file the item is so that we only pull back a Notebook(s).
The content Id that worked starts with: 0x0120
From what I’ve seen, each Notebook has the same ContentTypeId if they’re in the same site. I found it doesn’t match the Site Assets Id, Site Contents Id, or Site Id. However, they all start with that type to the best of my knowledge. Now we can apply this to the filter.
/_api/web/lists/getbytitle('Site Assets')/items?$filter=startswith(ContentTypeId,'0x0120')
Don’t forget to make sure ‘startswith’ is fully lowercase!
Note that spaces will be converted to %20 and single quotation marks as %27
Sneaky Content Types
Okay, now that we’ve gotten the items with that content type shouldn’t we be done? The crazy thing is that OneNote Notebooks are considered as folders so the ContentTypeId we’re sorting by is for ALL folders. This gets folders not considered OneNote Notebooks. The apparent difference I saw when figuring this out was the value for the title. (I read the xml) For a folder you get:
<d:Title m:null="true" />
For a Notebook you get the actual title:
<d:Title>Testing Notebook</d:Title>
So the most apparent thing to do is to add to our filter that the title shouldn’t equal null. The folder has ‘Testing Folder’ as it’s Name- but my best guess is that Name isn’t the same thing as a Title for Folders. (Feel free to comment if you know)
So let’s combine our filters! We can combine them by using ‘and’:
https://{TenantURL}/sites/${SiteName}/_api/web/lists/getbytitle('Site Assets')/items?$filter=startswith(ContentTypeId,'0x0120') and Title ne null
Wrap-up
That’s it! You can get your OneNote Notebooks from your SharePoint site using this hack. If you’re interested in more information on ContentTypeIDs the Microsoft Docs has plenty of information on them here. If you want to check out more information on the query parameters available check here and here.
Leave a Reply