{"id":2191,"date":"2018-11-25T11:00:04","date_gmt":"2018-11-25T11:00:04","guid":{"rendered":"https:\/\/developer.microsoft.com\/en-us\/office\/blogs\/?p=2191"},"modified":"2018-11-25T11:00:04","modified_gmt":"2018-11-25T11:00:04","slug":"30daysmsgraph-day-25-use-case-create-a-onenote-notebook","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/30daysmsgraph-day-25-use-case-create-a-onenote-notebook\/","title":{"rendered":"30DaysMSGraph \u2013 Day 25 \u2013 Use case: Create a OneNote notebook"},"content":{"rendered":"<p><a href=\"https:\/\/aka.ms\/30DaysMSGraph\">List of all posts in the #30DaysMSGraph series<\/a><\/p>\n<p>In <a href=\"https:\/\/developer.microsoft.com\/en-us\/graph\/blogs\/30daysmsgraph-day-24-use-case-calling-microsoft-graph-from-a-single-page-application-part-2\/\">Day 24<\/a> we concluded configuring implicit flow authentication on a single-page application.\u00a0 \u00a0Today we&#8217;ll extend the base console application to create a OneNote notebook, section, and page.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-2202\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day25_Source-1024x682.jpg\" alt=\"\" width=\"800\" height=\"533\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day25_Source-1024x682.jpg 1024w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day25_Source-300x200.jpg 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day25_Source-768x512.jpg 768w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day25_Source-1536x1024.jpg 1536w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day25_Source.jpg 2000w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>You can create OneNote notebooks on a variety of resources including the &#8220;\/me&#8221; endpoint, SharePoint Online team sites, SharePoint Online personal sites, and Office 365 Group sites.\u00a0 These correspond to the following endpoints:<\/p>\n<ul>\n<li>\/me\/onenote\/notebooks<\/li>\n<li>\/sites\/{id}\/onenote\/notebooks<\/li>\n<li>\/users\/{id | userPrincipalName}\/onenote\/notebooks<\/li>\n<li>\/groups\/{id | userPrincipalName}\/onenote\/notebooks<\/li>\n<\/ul>\n<p>In this post and associated code sample we are covering how to add a OneNote notebook, section, and page for a given user&#8217;s SharePoint Online personal site.\u00a0 The process can be applied to the other targets above with minor modifications.<\/p>\n<h3>Permissions required<\/h3>\n<p>OneNote notebooks can be created with several permissions.\u00a0 In today&#8217;s example we&#8217;ll be using application permissions for <strong>Notes.ReadWrite.All<\/strong> so that you can create and update notebooks for other users, but it is also possible to use delegated <strong>Notes.Create<\/strong> or <strong>Notes.ReadWrite<\/strong> for some of the same operations.<\/p>\n<p>Since you didn&#8217;t assign the <strong>Notes.ReadWrite.All<\/strong> permission in a previous day ensure that you assign that now to your Azure AD application.\u00a0 Navigate to the <a href=\"http:\/\/aka.ms\/appregistrationspreview\">Preview App Registrations<\/a> experience in the Azure AD portal and assign the application permission for <strong>Notes.ReadWrite.All<\/strong> under Microsoft Graph API.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-2203\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/OneNote1.png\" alt=\"\" width=\"800\" height=\"357\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/OneNote1.png 1019w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/OneNote1-300x134.png 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/OneNote1-768x343.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n<h3>Create a OneNote notebook<\/h3>\n<p>Start out by creating a OneNote notebook for a user.\u00a0 Since we are creating the notebook on a user&#8217;s SharePoint Online personal site we&#8217;ll reference the user by their UPN (user principal name) and then access the OneNote endpoint.<\/p>\n<pre style=\"padding-left: 30px\">public async <em>Task<\/em>&lt;<em>Notebook<\/em>&gt; CreateNoteBook(string upn, string notebookName)\n{\n\u00a0\u00a0\u00a0 var notebook = new <em>Notebook<\/em>{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DisplayName = notebookName\n\u00a0\u00a0\u00a0 };\n\n\u00a0\u00a0\u00a0 return (await _graphClient.Users[upn].Onenote.Notebooks.Request().AddAsync(notebook));\n}<\/pre>\n<h3>Create a OneNote section<\/h3>\n<p>After creating a OneNote notebook we keep a reference to the notebook created (or query for the notebook if it had already been created).\u00a0 Next create a section within the notebook.<\/p>\n<pre style=\"padding-left: 30px\">public async <em>Task<\/em>&lt;<em>OnenoteSection<\/em>&gt; CreateSection(string upn, <em>Notebook<\/em> notebook, string sectionName)\n{\n\u00a0\u00a0\u00a0 var section = new <em>OnenoteSection<\/em>{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 DisplayName = sectionName\n\u00a0\u00a0\u00a0 };\n\n\u00a0\u00a0\u00a0 return (await _graphClient.Users[upn].Onenote.Notebooks[notebook.Id].Sections.Request().AddAsync(section));\n}<\/pre>\n<h3>Create a OneNote page<\/h3>\n<p>The last step is creating a new page in the section.\u00a0 Interestingly enough you can reference the \/Pages endpoint directly from \/Users\/OneNote\/Sections without needing to go through the \/Notebooks endpoint.\u00a0 We are using the HttpClient to make the call against Microsoft Graph to show the versatility as well as conform to many of the examples you will see on Microsoft Graph and OneNote documentation.<\/p>\n<pre style=\"padding-left: 30px\">public async <em>Task<\/em>&lt;<em>HttpResponseMessage<\/em>&gt; CreatePage(string upn, <em>OnenoteSection<\/em> section, string pageName)\n{\n\u00a0\u00a0\u00a0\u00a0 <em>Uri<\/em> Uri = new <em>Uri<\/em>($\"https:\/\/graph.microsoft.com\/v1.0\/users\/{upn}\/onenote\/sections\/{section.Id}\/pages\");\n\n\u00a0\u00a0\u00a0 \/\/ use a verbatim interpolated string to represent the HTML text to be used for page creation\n\u00a0\u00a0\u00a0 var html = $@\"\n\u00a0\u00a0\u00a0 &lt;!DOCTYPE html&gt;\n\u00a0\u00a0\u00a0 &lt;html&gt;\n\u00a0\u00a0\u00a0 &lt;head&gt;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;title&gt;{pageName}&lt;\/title&gt;\n\u00a0\u00a0\u00a0 &lt;\/head&gt;\n\u00a0\u00a0\u00a0 &lt;body&gt;\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 I'm learning about the Microsoft Graph!\n\u00a0\u00a0\u00a0 &lt;\/body&gt;\n\u00a0\u00a0\u00a0 &lt;\/html&gt;\";\n\n\u00a0\u00a0\u00a0 <em>HttpContent<\/em> httpContent = new <em>StringContent<\/em>(html, System.Text.Encoding.UTF8, \"application\/xhtml+xml\");\n\n\u00a0\u00a0\u00a0 return (await _httpClient.PostAsync(Uri, httpContent));\n}<\/pre>\n<h2>Try It Out<\/h2>\n<p>Navigate to the\u00a0<a href=\"https:\/\/github.com\/microsoftgraph\/dotnetcore-console-sample\">dotnetcore-console-sample<\/a> repo.\u00a0 Do one (or both) of the following:<\/p>\n<p><a href=\"https:\/\/github.com\/microsoftgraph\/30DaysMSGraph-TryItOut\/blob\/master\/Day25-OneNote.md\">Day 25 repo link<\/a><\/p>\n<ol>\n<li>Clone the repo and configure the project in the Day 25 sub-folder.<\/li>\n<li>Follow the instructions in <a href=\"https:\/\/github.com\/microsoftgraph\/dotnetcore-console-sample\/tree\/master\/day25-onenote\">Day 25 sub-folder<\/a> to build the project from scratch yourself.<\/li>\n<\/ol>\n<p>If you run into any issues while building or configuring the project please create a new Issue on the repo.<\/p>\n<p>&nbsp;<\/p>\n<p>Join us tomorrow as we integrate Microsoft Flow with Microsoft Graph in <a href=\"https:\/\/developer.microsoft.com\/en-us\/graph\/blogs\/30daysmsgraph-day-26-use-case-calling-microsoft-graph-using-flow\/\">Day 26<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Day 24 we concluded configuring implicit flow authentication on a single-page application.\u00a0 \u00a0Today we&#8217;ll extend the base console application to create a OneNote notebook, section, and page.<\/p>\n","protected":false},"author":73055,"featured_media":25159,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[84],"class_list":["post-2191","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-graph","tag-30daysmsgraph"],"acf":[],"blog_post_summary":"<p>In Day 24 we concluded configuring implicit flow authentication on a single-page application.\u00a0 \u00a0Today we&#8217;ll extend the base console application to create a OneNote notebook, section, and page.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/2191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/users\/73055"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/comments?post=2191"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/2191\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media\/25159"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media?parent=2191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=2191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=2191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}