Automatic pagination for Graph API

Describe the problem/error/question

Hello everyone, I am creating a workflow for obtain Users from Microsoft graph.
For obtain users there is this endpoint: https://graph.microsoft.com/v1.0/users
And the limit is 999 contacts each request.
My database is 5k ish, and It’s growing, so we can say we don’t have a fixed number.

The pagination in Microsoft Graph works with a page.link, that it’s provided in the first request you do.
Then you need to use this link for scroll the page and search.

What is the error message (if any)?

There is no error, but I need to find a proper way to scroll all the users I am obtaining from the first request.

My idea was to obtain every time the
{{ $(‘Get Users’).item.json[“value”].length }} (number of users obtained from the call)
and compare with
{{ $(‘Get Users’).item.json[“@odata.count”] }} (number of all the users)

Do you have any idea?
Let me know if you need anythings

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 0.231.3
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @Gabriele_Bracciali, Microsoft suggests over here to use @odata.nextLink as the URL for the next page.

So I’d go for something like this:

This will simply use the @odata.nextLink as the URL until it’s no longer provided by the API.

2 Likes

Hi @MutedJam , thank for your help like always.

So I should add this part after the first call “Get users” without the nextLink?

Hi @Gabriele_Bracciali, you can start with this part. It should fetch all users, no other HTTP Request nodes needed :slight_smile:

It does this using this expression: {{ $json['@odata.nextLink'] || "https://graph.microsoft.com/v1.0/users?$top=10&$count=true" }}

When @odata.nextLink is available it’d use that URL, but when not (so on the very first run), it’d go with https://graph.microsoft.com/v1.0/users?$top=10&$count=true instead. This is called short-circuiting.

I’ve removed your original filter in my example though as your filter wouldn’t match any of my users. So you probably want to add this piece back in.

1 Like

hi @MutedJam , looks working like that, thank you so much.
I will let you know if I have any other question.

Have a good one

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.