admin管理员组

文章数量:1125039

I'm using the Graph SDK in C# to get a list of PSTN Call Logs. Some of my date ranges return over 1000 and results are on multiple pages. I'm not sure of the best way to iterate through the results in order to retrieve all records.

UPDATE: I'm able to "hack" my way through a solution but I know this is not the way to properly iterate through the pages. I set up an integer and default to 0. First time hitting it, I tell it to skip i*1000.

var response = await graphClient.Communications.CallRecords.MicrosoftGraphCallRecordsGetPstnCallsWithFromDateTimeWithToDateTime(DateTimeOffset.Parse("2024-12-18"), DateTimeOffset.Parse("2024-12-28"))
            .GetAsGetPstnCallsWithFromDateTimeWithToDateTimeGetResponseAsync(requestConfiguration =>
            {
                requestConfiguration.QueryParameters.Skip = i * 1000;
            }
        );

Then I do a while loop looking for ODataNextLink and increment i.

There has to be a proper way to page through the results using the SDK but I'm not sure of how to do it. Here's the full code that I have running now and would appreciate if someone can share how to properly iterate through the results.

// Multi-tenant apps can use "common",
        // single-tenant apps must use the tenant ID from the Azure portal
        var tenantId = "xxx";

        // Value from app registration
        var clientId = "xxx";

        var dotNetClientSecret = "xxx";

        var tokenCredential = new ClientSecretCredential(
            tenantId,
            clientId,
            dotNetClientSecret
            );


        // NOTE: Authentication requests will not go through the proxy.
        // Azure.Identity token credential classes have their own separate method
        // for configuring a proxy using TokenCredentialOptions.Transport
        var authProvider = new AzureIdentityAuthenticationProvider(tokenCredential, null, null, isCaeEnabled: true);

        
        var graphClient = new GraphServiceClient(authProvider);

        List<Microsoft.Graph.Models.CallRecords.PstnCallLogRow> callLogs = new List<Microsoft.Graph.Models.CallRecords.PstnCallLogRow>();

        int i = 0;

        var response = await graphClient.Communications.CallRecords.MicrosoftGraphCallRecordsGetPstnCallsWithFromDateTimeWithToDateTime(DateTimeOffset.Parse("2024-12-18"), DateTimeOffset.Parse("2024-12-28"))
            .GetAsGetPstnCallsWithFromDateTimeWithToDateTimeGetResponseAsync(requestConfiguration =>
            {
                requestConfiguration.QueryParameters.Skip = i * 1000;
            }
        );

        var pstnCalls = response.Value;

        var nextLink = response.OdataNextLink;


        while (!string.IsNullOrEmpty(nextLink))
        {
            i += 1;

            response = await graphClient.Communications.CallRecords.MicrosoftGraphCallRecordsGetPstnCallsWithFromDateTimeWithToDateTime(DateTimeOffset.Parse("2024-12-18"), DateTimeOffset.Parse("2024-12-28"))
                .GetAsGetPstnCallsWithFromDateTimeWithToDateTimeGetResponseAsync(requestConfiguration =>
                {
                    requestConfiguration.QueryParameters.Skip = i * 1000;
                }
            );

            pstnCalls.AddRange(response.Value);

            nextLink = response.OdataNextLink;
        }

本文标签: C Graph PstnCallLogRow OdataNextLinkStack Overflow