Search code examples
c#uwpmicrosoft-graph-apionenote-apimicrosoft-graph-sdks

How do I retrieve the content of a OneNote page using the Microsoft Graph .NET SDK?


I'm swapping out my usage of the old OneDrive SDK to programmatically access the contents of OneNote in a UWP app, and I've begun using the Microsoft Graph for .NET SDK. One of the things I need to do is get a specific page's content. Currently, I'm attempting to do so like this:

await _graphClient.Me.Onenote.Pages
    .Request()
    .Filter("tolower(title) eq 'shopping list'")                    
    .GetAsync();

...which works, and gets me all the pages in my notebook with the title of "shopping list". However, all of those pages have a null Content property, leaving me unable to get the HTML content of those pages. I can verify that at least one of these pages does, in fact, have content in the OneNote application.

I've read through the documentation for the SDK, and it appears that I should just be getting a Stream back without any further action. Is there a step I'm missing, or am I using the API incorrectly?


Solution

  • It turns out that the SDK supports getting an individual page's Content directly, but the syntax to do so isn't very discoverable. It is:

    Stream pageContent = await _graphClient.Me.Onenote.Pages[page.Id]
        .Content // This could be omitted to retrieve every property on the page, presumably
        .Request()
        .GetAsync();
    

    Turns out an IOnenotePagesCollectionRequestBuilder returned by _graphClient.Me.Onenote.Pages supports key-style indexing to retrieve a specific page. Totally unfindable with the default intellisense dropdown =/