Search code examples
c#onenote-api

OneNote API Create Page C#


I am trying to create new pages through the OneNote API. My question is, do I have to use the Live SDK to create a new page or can I just use the access token I got from my OneNote API Dekstop Applications? Here is the code I have for creating the new page:

        private async Task CreateTestPage()
        {
            try
            {
                var client = new HttpClient();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "tokenString");


                string sampleHtml = @"<html>
    <head>
        <title>A sample page</title> 
    </head>
    <body>  
        <p>This is a page that has some info</p>
    </body>
</html>";

                var createMessage = new HttpRequestMessage(HttpMethod.Post, _pagesEndPOint)
                {
                    Content = new StringContent(sampleHtml, Encoding.UTF8, "text/html")
                };

                HttpResponseMessage response = await client.SendAsync(createMessage);
                response = await client.SendAsync(createMessage);

                await response.Content.ReadAsStringAsync();   
            }
            catch (Exception ex) { }
        }

I am not getting any errors thrown, but it is not creating any new page when I run the application. Also, I am creating this in a Word Add in so I call this method CreateTaskPage() inside the Add In method.

Any help on getting this up and running would be VERY helpful, thanks :D


Solution

  • I recommend you use the MS Graph API and SDKs - you can do everything you can with the OneNote API with it, but their SDKs/docs are more extensive (they even have C# SDKs)

    You can try it out in the console (check the OneNote section)

    https://developer.microsoft.com/en-us/graph/graph-explorer