{"id":1989,"date":"2018-11-07T10:00:27","date_gmt":"2018-11-07T10:00:27","guid":{"rendered":"https:\/\/developer.microsoft.com\/en-us\/office\/blogs\/?p=1989"},"modified":"2018-11-07T10:00:27","modified_gmt":"2018-11-07T10:00:27","slug":"30daysmsgraph-day-7-paging-and-nextlink","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/30daysmsgraph-day-7-paging-and-nextlink\/","title":{"rendered":"30DaysMSGraph \u2013 Day 7 \u2013 Paging and NextLink"},"content":{"rendered":"<p><a href=\"https:\/\/aka.ms\/30DaysMSGraph\">List of all posts in the #30DaysMSGraph series<\/a><\/p>\n<p>-Today&#8217;s post written by Srinivas Varukala<\/p>\n<p>In <a href=\"https:\/\/developer.microsoft.com\/en-us\/graph\/blogs\/30daysmsgraph-day-6-query-parameters-part-2\/\">Day 6<\/a> we finished discussing many of the query parameters available for Microsoft Graph requests.\u00a0 Today we&#8217;ll look at the concept of paging results when there are more records than can be returned in a single response.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-1993\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day7_Source-300x225.jpg\" alt=\"\" width=\"500\" height=\"375\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day7_Source-300x225.jpg 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day7_Source-1024x768.jpg 1024w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day7_Source-768x576.jpg 768w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2018\/11\/30DaysMSGraph_Day7_Source.jpg 1280w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/p>\n<h3><a href=\"https:\/\/developer.microsoft.com\/en-us\/graph\/docs\/concepts\/paging\">NextLink<\/a><\/h3>\n<p>Microsoft Graph returns multiple pages of data when you issue a query that requires pagination. This could happen either due to server-side paging or due to the query using the <strong>$top<\/strong> query parameter to restrict the number of records that are returned. In either case, Microsoft Graph returns an <strong>@odata.nextLink<\/strong> property in the response that contains the URL to the next page of results.<\/p>\n<p>You can use the link that is provided in the <strong>@odata.nextLink<\/strong> to get the next page of results.<\/p>\n<p>To see this in action navigate to the <a href=\"https:\/\/aka.ms\/ge\">Graph Explorer<\/a> and issue a query to get back all the users in your organization along with the $top query parameter:<\/p>\n<p><a href=\"https:\/\/graph.microsoft.com\/v1.0\/users?$top=3\">https:\/\/graph.microsoft.com\/v1.0\/users?$top=3<\/a><\/p>\n<p>Sample response value (truncated for brevity) is as follows:<\/p>\n<p>{\n&#8220;@odata.context&#8221;: &#8220;https:\/\/graph.microsoft.com\/v1.0\/$metadata#users&#8221;,\n&#8220;@odata.nextLink&#8221;: &#8220;https:\/\/graph.microsoft.com\/v1.0\/users?$top=3&amp;$skiptoken=X%2744537074020001000000223A416C657857404D333635783939353035322E4F6E4D6963726F736F66742E636F6D29557365725F33613166623664392D373666342D346233352D623139332D333764643136373530396233B900000000000000000000%27&#8221;,\n&#8220;value&#8221;: [\n{\n&#8220;id&#8221;: &#8220;c25b1eaf-eb49-4f66-90a1-aa41be4d993a&#8221;,\n&#8220;displayName&#8221;: &#8220;Adele Vance&#8221;,\n&#8220;givenName&#8221;: &#8220;Adele&#8221;,\n&#8220;userPrincipalName&#8221;: &#8220;AdeleV@M365x995052.OnMicrosoft.com&#8221;\n},\n{\n\u2026\n},\n{\n\u2026\n}\n]\n}<\/p>\n<p>The response contains the <strong>@odata.nextLink<\/strong> property that contains a URL (Microsoft Graph query) with an additional <strong>$skiptoken<\/strong> query parameter. Using this URL you can re-issue another query within the Graph Explorer to get the next page results.<\/p>\n<p>Microsoft Graph will continue to return the <strong>@odata.nextLink<\/strong> property in each response until all pages of the result has been read.<\/p>\n<p>A few important things to note:<\/p>\n<ul>\n<li>Any collection can be paginated, even collections of strings.<\/li>\n<li>Collections might appear unpaginated initially because they don&#8217;t have enough items to return a NextLink.<\/li>\n<li>There are a few APIs that may return errors if you ask for too many items with $top (ex. Azure AD users &#8220;\/users&#8221;, SharePoint list items &#8220;\/items&#8221;, etc.)<\/li>\n<li>The <strong>@odata.nextLink<\/strong> returned will always retain all of the original query parameters and should not be modified.<\/li>\n<li>Depending on the resource (example: Users, SharePoint list etc.) that you are querying against, the <strong>@odata.nextLink<\/strong> will contain either $skipToken or a $skip query parameter.<\/li>\n<li>Not all resources or relationships support paging. For example, queries against <a href=\"https:\/\/developer.microsoft.com\/en-us\/graph\/docs\/api-reference\/v1.0\/resources\/directoryrole\">directoryRoles<\/a> do not support paging.<\/li>\n<li>It is recommended to prepare for all arrays to be paginated and write your code \/ query accordingly.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Try It Out<\/h2>\n<p>Navigate to the <a href=\"https:\/\/aka.ms\/ge\">Graph Explorer<\/a>.\u00a0 Execute the following commands.<\/p>\n<p><a href=\"https:\/\/github.com\/microsoftgraph\/30DaysMSGraph-TryItOut\/blob\/master\/Day07-PagingNextLink.md\">Day 7 repo link<\/a><\/p>\n<ol>\n<li>Get the users from your organization\u2019s directory\n<ul>\n<li>https:\/\/graph.microsoft.com\/v1.0\/users?$top=10<\/li>\n<\/ul>\n<\/li>\n<li>Get all the items in your OneDrive for Business\n<ul>\n<li>https:\/\/graph.microsoft.com\/v1.0\/me\/drive\/root\/children?$top=5<\/li>\n<\/ul>\n<\/li>\n<li>Get all the items from a SharePoint Online Site list\n<ul>\n<li><a href=\"https:\/\/graph.microsoft.com\/v1.0\/sites%20\/m365x214355.sharepoint.com:\/sites\/HR\/lists\/c23b10c5-c190-45f2-bc9b-aabe72f87d08\/items?$top=5000\">https:\/\/graph.microsoft.com\/v1.0\/sites \/m365x214355.sharepoint.com:\/sites\/HR:\/lists\/ ed70c81d-fedf-4a1b-a1f1-f44e39c5bb78\/items?$top=5000<\/a><\/li>\n<li>See Appendix for details on how to build this Microsoft Graph query for SharePoint<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>Join us tomorrow as we review authenticating to Microsoft Graph and specifically access tokens in <a href=\"https:\/\/developer.microsoft.com\/en-us\/graph\/blogs\/30daysmsgraph-day-8-authentication-roadmap-and-access-tokens\">Day 8<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<h2>Appendix<\/h2>\n<p>How to get SharePoint List Items using Microsoft Graph<\/p>\n<ol>\n<li>If you are not logged into the Graph Explorer (i.e. using a demo tenant) find out the name of the tenant. Issue the following Microsoft Graph query:\n<ul>\n<li>https:\/\/graph.microsoft.com\/v1.0\/sites\/root<\/li>\n<li>Copy the <strong>hostname<\/strong> value from siteCollection entity, ex. &#8220;m365x214355.sharepoint.com&#8221;<\/li>\n<\/ul>\n<\/li>\n<li>Next you must use the site collection relative URL and find out the <strong>id<\/strong> of a list on that site. Issue below Microsoft Graph query to get those details:\n<ul>\n<li>https:\/\/graph.microsoft.com\/v1.0\/sites\/&lt;hostname value from step 1 above&gt;:\/&lt;relative path to site&gt;:\/lists\n<ul>\n<li>Ex. https:\/\/graph.microsoft.com\/v1.0\/sites\/m365x214355.sharepoint.com:\/sites\/HR:\/lists<\/li>\n<\/ul>\n<\/li>\n<li>Copy the <strong>{id}<\/strong> value from the first list record shown.\n<ul>\n<li>Ex. &#8220;id&#8221;: &#8220;ed70c81d-fedf-4a1b-a1f1-f44e39c5bb78&#8221;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>Next create Microsoft Graph query to fetch the top 5000 items (max that SharePoint can return in one page) from the list\n<ul>\n<li>https:\/\/graph.microsoft.com\/v1.0\/sites\/&lt;hostname value from step 1&gt;:\/&lt;relative path to site&gt;:\/lists\/&lt;id value from step 2&gt;\/items?$top=5000\n<ul>\n<li>Ex. https:\/\/graph.microsoft.com\/v1.0\/sites\/m365x214355.sharepoint.com:\/sites\/HR:\/lists\/ed70c81d-fedf-4a1b-a1f1-f44e39c5bb78\/items?$top=5000<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In Day 6 we finished discussing many of the query parameters available for Microsoft Graph requests.\u00a0 Today we&#8217;ll look at the concept of paging results when there are more records than can be returned in a single response.<\/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-1989","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-graph","tag-30daysmsgraph"],"acf":[],"blog_post_summary":"<p>In Day 6 we finished discussing many of the query parameters available for Microsoft Graph requests.\u00a0 Today we&#8217;ll look at the concept of paging results when there are more records than can be returned in a single response.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/1989","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=1989"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/1989\/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=1989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=1989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=1989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}