A development has been carried out in C# to attract two Microsoft Graph queries. The code works perfectly, but it only brings me 100 devices out of the 2000 that I have. So, in some way, I want to take the Odata.nexlink variables to bring the rest of the devices through the code, but I have not yet found the answer. I want this query to bring me the 2000 devices.
using System;
using System.Linq;
using System.Threading.Tasks;
using Azure.Identity;
using Microsoft.Graph;
using Newtonsoft.Json;
using Microsoft.Kiota.Http.HttpClientLibrary;
namespace new_project
{
public class ResponseData
{
public string DeviceId { get; set; }
public string DeviceName { get; set; }
//public List<string> PolicyNames { get; set; }
public string OperatingSystem { get; set; }
public ResponseData(string deviceId, string deviceName, string operatingSystem)
{
DeviceId = deviceId;
//this.PolicyNames = policyNames;
DeviceName = deviceName;
OperatingSystem = operatingSystem;
}
}
class Program
{
static async Task Main(string[] args)
{
// Authentication section for the created application in Azure
string[] scopes = { "https://graph.microsoft.com/.default" };
var clientId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var tenantId = "xxxxxxxxxxxxxxxxxxxx";
var clientSecret = "xxxxxxxxxxxxxxxxxxxxx";
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var responseList = new List<ResponseData>();
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
// Microsoft Graph queries
var devices = await graphClient.Devices.GetAsync();
// Corrected loop to iterate over pages of devices
while (devices.OdataNextLink != null)
{
// Process the current page of devices
foreach (var device in devices.Value)
{
var deviceId = device.DeviceId.ToString();
var displayName = device.DisplayName.ToString();
var operatingSystem = device.OperatingSystem.ToString();
try
{
responseList.Add(new ResponseData(deviceId, displayName, operatingSystem));
}
catch (Exception ex)
{
Console.WriteLine($"Error getting device compliance policy states for device {deviceId}: {ex.Message}");
}
}
// Get the next page of devices
devices = await graphClient.Devices.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Top = 50;
requestConfiguration.QueryParameters.Skiptoken = "RFNwdAIAAQAAACtEZXZpY2VfMDFiN2QyYzEtMmE1My00YzdlLTlkMjYtOGMyOTgxZjYxMDNkK0RldmljZV8wMWI3ZDJjMS0yYTUzLTRjN2UtOWQyNi04YzI5ODFmNjEwM2QAAAAAAAAAAAAAAA";
});
}
Console.WriteLine(JsonConvert.SerializeObject(responseList, Formatting.Indented));
}
}
}