简体   繁体   中英

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?

Page content could be requested per single page, for example:

GET https://graph.microsoft.com/v1.0/me/onenote/pages/{page-id}/content

Per collection of pages content could be requested like this (via contentUrl property of Page resource ):

var result = await graphClient.Me.Onenote.Pages.Request().GetAsync();
foreach (var page in result)
{

     //download Page content
     var message = new HttpRequestMessage(HttpMethod.Get, page.ContentUrl);
     await graphClient.AuthenticationProvider.AuthenticateRequestAsync(message);
     var response = await graphClient.HttpProvider.SendAsync(message);
     var content = await response.Content.ReadAsStringAsync();  //get content as HTML 

}

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 =/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM