{"id":1270,"date":"2017-07-21T21:59:51","date_gmt":"2017-07-22T04:59:51","guid":{"rendered":"https:\/\/officedevblogs.wpengine.com\/?p=1270"},"modified":"2021-10-18T13:22:25","modified_gmt":"2021-10-18T20:22:25","slug":"a-few-performance-tips-for-using-the-onenote-api","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/a-few-performance-tips-for-using-the-onenote-api\/","title":{"rendered":"A few performance tips for using the OneNote API"},"content":{"rendered":"<p>Hello world,<\/p>\n<p>In StackOverflow and twitter, we often hear questions on how to make queries to the API faster. Here are a few recommendations:<\/p>\n<h3>Always log the &#8220;X-CorrelationId&#8221;, &#8220;Date&#8221; and &#8220;Request-Processing-Time&#8221; headers on the OneNote API response<\/h3>\n<p>First &#8211; make sure you have the API&#8217;s performance-related information logged. All requests to the OneNote API have those 3 headers &#8211; here&#8217;s an example:<\/p>\n<pre>HTTP\/1.1 200 OK\nCache-Control: no-cache\nPragma: no-cache\nContent-Type: application\/xml\nExpires: -1\nServer: Microsoft-IIS\/10.0\n<strong>X-CorrelationId: d0ff1746-6b9d-456f-ae18-3c7bc822d53e<\/strong>\nX-UserSessionId: d0ff1746-6b9d-456f-ae18-3c7bc822d53e\nX-OfficeFE: deployment29(8).MSOffice.ClassNotebookApiSingleBox_IN_0\nX-OfficeVersion: 16.0.8418.3004\nX-OfficeCluster: localhost\nP3P: CP=\"CAO DSP COR ADMa DEV CONi TELi CUR PSA PSD TAI IVDi OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR\"\nX-Content-Type-Options: nosniff\n<strong>Request-Processing-Time: 838.9842 ms<\/strong>\nOData-Version: 4.0\nX-AspNet-Version: 4.0.30319\nX-Powered-By: ASP.NET\n<strong>Date: Fri, 28 Jul 2017 16:54:58 GMT<\/strong>\nContent-Length: 32552<\/pre>\n<p>X-CorrelationId: This uniquely identifies any API request. In case there are any issues with\u00a0the API, we can use this for debugging.\nRequest-Processing-Time: Latency of the API.\nDate: The date when the request was made.<\/p>\n<p>Having this in your logs will make debugging performance issues easier.<\/p>\n<h3>Use $select to select the minimum set of properties you need<\/h3>\n<p>When querying for a resource (say for example, sections inside a notebook), you make a request like:<\/p>\n<pre>GET ~\/notebooks\/{id}\/sections<\/pre>\n<p>This retrieves all the properties of the sections &#8211; but perhaps you don&#8217;t really need them all, maybe you only need the id and the name of the section&#8230; in that case, it is better to say:<\/p>\n<pre>GET ~\/notebooks\/{id}\/sections?$select=id,name<\/pre>\n<p>The same applies to other\u00a0APIs &#8211; the fewer properties you select, the better.<\/p>\n<h3>Use $expand instead of making multiple API calls<\/h3>\n<p>Suppose you want to retrieve all of the user\u2019s notebooks, sections, and section groups in a hierarchical view.<\/p>\n<p>It can be tempting to do the following:<\/p>\n<ol>\n<li>Call GET ~\/notebooks to get the list of notebooks<\/li>\n<li>For every retrieved notebook, call GET ~\/notebooks\/{notebookId}\/sections to retrieve the list of sections<\/li>\n<li>For every retrieved notebook, call GET ~\/notebooks\/{notebookId}\/sectionGroups to retrieve the list of section groups<\/li>\n<li>\u2026 optionally recursively iterate through section groups<\/li>\n<\/ol>\n<p>While this will work (with a few extra sequential roundtrips to our service), there is a much better alternative.\u00a0<a title=\"Expand blog post\" href=\"http:\/\/blogs.msdn.com\/b\/onenotedev\/archive\/2014\/06\/12\/create-page-in-specific-location-now-in-beta.aspx\">See our blog post about expand<\/a>\u00a0for a more details.<\/p>\n<pre>GET ~\/api\/v1.0\/me\/notes\/notebooks?$expand=sections,sectionGroups($expand=sections)<\/pre>\n<p>This will yield the same results in one network roundtrip with way better performance \u2013 yay!<\/p>\n<h3>When getting all pages for\u00a0a user, do so for each section separately<\/h3>\n<p>While we do expose an endpoint to retrieve all pages, this isn&#8217;t the best way to get all the pages the user has access to &#8211; when the user has too many sections, this can lead to timeouts\/bad performance. It is better to iterate each section, getting pages for each one separately.<\/p>\n<p>So it is significantly better to do several (especially if you don&#8217;t need all sections):<\/p>\n<pre>GET ~\/sections\/{id}\/pages<\/pre>\n<p>Than several (this API is paged, so you won&#8217;t be able to fetch them all at once):<\/p>\n<pre>GET ~\/pages<\/pre>\n<h3>When getting page metadata, override default lastModifiedDate ordering<\/h3>\n<p>It is faster for us to get pages when we don&#8217;t have to sort them by lastModifiedDate (which is the default ordering) &#8211; you can achieve this by sorting by any other property:<\/p>\n<pre>GET ~\/sections\/{id}\/pages?$select=id,name,creationDate&amp;$orderby=creationDate<\/pre>\n<p>Happy coding,\nJorge<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello world, In StackOverflow and twitter, we often hear questions on how to make queries to the API faster. Here are a few recommendations: Always log the &#8220;X-CorrelationId&#8221;, &#8220;Date&#8221; and &#8220;Request-Processing-Time&#8221; headers on the OneNote API response First &#8211; make sure you have the API&#8217;s performance-related information logged. All requests to the OneNote API have [&hellip;]<\/p>\n","protected":false},"author":69223,"featured_media":25159,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11],"tags":[71],"class_list":["post-1270","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-office-add-ins","tag-onenote"],"acf":[],"blog_post_summary":"<p>Hello world, In StackOverflow and twitter, we often hear questions on how to make queries to the API faster. Here are a few recommendations: Always log the &#8220;X-CorrelationId&#8221;, &#8220;Date&#8221; and &#8220;Request-Processing-Time&#8221; headers on the OneNote API response First &#8211; make sure you have the API&#8217;s performance-related information logged. All requests to the OneNote API have [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/1270","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\/69223"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/comments?post=1270"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/1270\/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=1270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=1270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=1270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}