Search code examples
azure-active-directorymicrosoft-graph-api

How to overcome the limit of records returned in Microsoft Graph?


I sent the following request using postman:

https://graph.microsoft.com/v1.0/auditLogs/signIns

And the response was of 1000 records. Is there a way to overcome the limit of records? I tried:

https://graph.microsoft.com/v1.0/auditLogs/signIns?$top=5000

I retrieve the same result. Only 1000 records. Is there a way to overcome that limit?. Thanks in advance.


Solution

  • The $top query parameter is not intended to be an alternative to data paging. It is intended to control the size of each page, and it has an upper limit of 999 records per page. From the documentation:

    $top accepts a minimum value of 1 and a maximum value of 999 (inclusive).

    The default page size for most endpoints is 100 records. For most use cases, this default should be left at the default. There are exceptions, but most of the time it is to reduce the page size (i.e. optimized for low memory or mobile devices).

    Instead of setting massive page sizes, you should iterate through the paged data returned by Graph. This process is outlined in Paging Microsoft Graph data in your app. For example, using C# you might do something along these lines:

    // Create a bucket to hold the final results
    var userList = new List<User>(); 
    
    // Request the first page of data
    var usersPage = await graphServiceClient
        .Users
        .GetAsync();
    
    // Add the first page of data to the final list
    userList.AddRange(usersPage.CurrentPage);
    
    // Repeate until all pages have been returned
    while (usersPage.NextPageRequest != null)
    {
        usersPage = await usersPage.NextPageRequest.GetAsync();
        userList.AddRange(usersPage.CurrentPage);
    }