Search code examples
c#onedrivemicrosoft-graph-api

Microsoft Graph C# SDK - How to return a Stream for OneNote?


I am coding against the Microsoft Graph C# SDK and I am looking to return a stream for a Package facet. More specifically I would like to return a stream for a OneNote.

After reading the docs at: Package Facet. Seems that a Package can be understood as a folder. My question is how would someone go about downloading a stream with the .Content() for a OneNote file?


Solution

  • This scenario doesn't appear to be supported. You can use the current .Net Graph client library to get the metadata for the Notebook (the first GetAsync call), but the second GetAsync call to get the content results in 404. With this said, the expected way to interact with OneNote is through the OneNote API which is in the beta endpoint. We don't have a client library for the new features in the beta endpoint at this point so you'll need to make the calls outside of the .Net client library.

    // Get the OneNote notebook identifier here.
    var driveItems = await graphClient.Me.Drive.Root.ItemWithPath("Notebooks").Children.Request().GetAsync();
    
    foreach (var item in driveItems)
    {
        // Let's download the first file we get in the response.
        if (item.Package.Type == "oneNote")
        {
            // Fail 404, response body error is UnknownError.
            var driveItemContent = await graphClient.Me.Drive.Items[item.Id].Content.Request().GetAsync();
            return;
        }
    }
    

    You'll want to use the following to get the OneNote notebook:

    GET /me/notes/notebooks/{notebookidentifier}
    

    That should get you into your notebook so that you can drill into the sections and pages. Please note that this API is still in beta and changes may occur.