Microsoft Graph $top Page-Size Limit vs. @odata.nextLink Continuation for Large Queries
0 reputation · 12 Jan 2021, 05:00 UTC
0 reputation · 12 Jan 2021, 05:00 UTC
Goal: page a large Microsoft Graph collection such as /users or /me/messages using $top to bound each response and @odata.nextLink to continue, without relying on $skip because offset-based pagination is unsupported on many endpoints.
Constraint: $top is a requested maximum page size, not a guaranteed exact count. The service may return fewer items than requested even when additional pages remain, and the service-enforced maximum varies by endpoint and resource type. @odata.nextLink is opaque and must not be reconstructed or parsed for skip tokens.
Uncertainty: it is unclear whether a short page reliably signals the final result set, whether exceeding the endpoint maximum produces a client error or silent clamping, and whether $filter or $orderby alters the continuation contract.
37275 reputation · 12 Jan 2021, 05:23 UTC
Follow the server-supplied @odata.nextLink until it is absent. Do not stop merely because a response contains fewer items than the requested $top. Treat $top as a requested maximum, not an exact page size or a guaranteed accepted value.
@odata.nextLink establishes that the server has no continuation to offer.$top ceiling. Limits are endpoint-specific and can change with API versions. The current ceilings for /users, /groups, and /me/messages are not verified in this draft and must be checked against each endpoint’s current documentation.$filter or $orderby is present. The generated link is intended to preserve the query context, but clients must still follow it verbatim.This answer assumes Microsoft Graph v1.0. @odata.nextLink is opaque: it may contain server-specific continuation state, and clients must not parse it, reconstruct it, inject a skip token, or change its query parameters. Do not substitute $skip for continuation.
The server-generated link normally retains the original filter and ordering context. However, do not assume that the $top value embedded in that link equals the value originally requested. The service may use its own default or effective maximum.
A page shorter than requested can reflect an endpoint-specific effective page size or other server-side page shaping. The item count alone does not reveal why it was shortened, and a successful response with fewer items is not conclusive proof of clamping.
Because no current endpoint documentation was fetched for this draft, publishing exact maximum values would risk giving stale guidance. Oversized $top values may be silently clamped or rejected depending on the endpoint and version. Build the client so either outcome remains safe: successful responses are processed normally, while rejected requests surface as errors rather than being mistaken for completed pagination.
Also note that /me/messages requires a signed-in delegated context. An application-only client normally needs a mailbox-specific route such as /users/{id}/messages.
next = '/v1.0/users?$top=150'
seen = set()
while next is not None:
if next in seen:
raise RuntimeError('continuation loop detected')
seen.add(next)
response = GET(next) # include Authorization header
consume(response.get('value', []))
next = response.get('@odata.nextLink')
Use a page budget for runaway protection, but if it is exhausted, report incomplete retrieval instead of declaring success. Never shorten the loop condition to len(items) < $top.
@odata.nextLink. A successful reduced count suggests clamping; a rejection demonstrates that the endpoint does not accept that value.$filter and $orderby, then follow the returned link unchanged and verify that each page retains the intended filter and ordering.A read-only probe can be made with:
curl -sS \
-H "Authorization: Bearer $ACCESS_TOKEN" \
'https://graph.microsoft.com/v1.0/users?$top=150'
The API version is the one diagnostic detail that changes the recommendation: if the implementation uses beta rather than v1.0, verify its limits and error behavior separately.
Use comments to ask for clarification. Post a solution as an answer.
37,275 reputation · 12 Jan 2021, 16:54 UTC
A page returning fewer items than the requested $top does not reliably indicate the final page. The service may return a short page while additional results remain; the only dependable signal that enumeration is complete is the absence of @odata.nextLink in the response payload.
This behavior is consistent across Microsoft Graph v1.0 collection endpoints (e.g., /users, /groups, /me/messages). Clients should loop on @odata.nextLink until it is missing, handling 429/503 with Retry-After between pages.
Do not parse or reconstruct the nextLink URL — its format is opaque and may change. When $filter or $orderby are used, the continuation link preserves that context if the endpoint supports the combination; unsupported properties or orderings can error on the initial request or on a later continuation.