Paging in Microsoft Graph REST API

I have written a Blog Series, on how to use Microsoft Graph REST API

Part 1 – Authentication and Azure App – Use Microsoft Graph API with PowerShell – Part 1 » TechGuy
Part 2 – Oauth2.0 – Use Microsoft Graph API with PowerShell – Part 2 » TechGuy
Part 3 – First Powershell Script to get a Teams Lis and Walkthrough – Use Microsoft Graph API with PowerShell – Part 3 » TechGuy
Part 4 – many examples – Use Microsoft Graph API with PowerShell – Part 4 » TechGuy

Now, i want to show you an example, how you can work with paging.

For example, to retrieve all your Users from API, you need to call “GET /users.” And if you are in a larger company, you will receive many 100 or many 1000 Entries.

Microsoft GRAPH REST API is not sending that many Objects at once. You have to change your Query a little bit.

To talk to Microsoft GRAPH REST API and authorize, see my previous post on the Top, which we will not care about here

So, we suppose that the Variable $header has all information like described in the post before.

Our Example, as already mentioned above is, to Query all User Objects.

So, without any Paging, the Code is this

$Uri = "https://graph.microsoft.com/v1.0/users"

$UserResponse = Invoke-RestMethod -Uri $uri -Headers $headers -Method Get -Verbose

$UserResponse.value.count

The Result, so the amount of Users we get is: 100

Trust me, we have more Users than this.

There is a Support Artivel about Paging: Paging Microsoft Graph data in your app – Microsoft Graph | Microsoft Docs

So, to get the Response, if we need Paging or not, there is a Response from the API called “@odata.nextLink”

With this information we know, how long we need to run the Request again and what URL we need to call.

So the Code will look like this.

$Uri = "https://graph.microsoft.com/v1.0/users"
$UserResponse = Invoke-RestMethod -Uri $uri -Headers $headers -Method Get -Verbose

$CloudUser = $UserResponse.value 
$UserNextLink = $UserResponse."@odata.nextLink"


while ($UserNextLink -ne $null) {

    $UserResponse = (Invoke-RestMethod -Uri $UserNextLink -Headers $headers -Method Get -Verbose)
    $UserNextLink = $UserResponse."@odata.nextLink"
    $CloudUser += $UserResponse.value

}

$CloudUser.count

And the Result is: 1154

Now, we received all User with Paging.

Michael Seidl aka Techguy

4 thoughts on “Paging in Microsoft Graph REST API”

  1. Pingback: Catch up on the latest Azure Monitor, SCOM and SquaredUp news | SquaredUp

  2. Pingback: Community Round-Up: February 2021 - SquaredUp DS

  3. Pingback: Community Round-Up: February 2021 - SquaredUp DS

  4. Pingback: MS Graph API Paging Function - TechGuy

Leave a Comment

Your email address will not be published. Required fields are marked *

*