Microsoft Graph CSharp SDK - 获取DriveItems-分页无法正常工作,只能检索1000个项目中的200个项目

时间:2017-03-28 22:11:33

标签: c# microsoft-graph onedrive

我在测试我的群发收集申请时正在对Microsoft Graph CSharp SDK进行编码。我注意到我的代码存在分页问题。我在测试OneDrive帐户中添加了1000个1kb文件 - 然后我运行了我的代码以查看是否可以访问所有文件。不幸的是,我只能在一个文件夹中看到200个文件 - 而不是完整的1000个文件。

我在网上找到了这个GitHub(现已关闭)bug

在逐步执行我的代码时,我发现NextPageRequestnull。我只是想确保没有错误,并确保我正确使用NextPageRequest

代码:

public Collection<DriveItem> GetSubItems(string id, string username)
{
    Collection<DriveItem> response = new Collection<DriveItem>();
    var retryCount = 0;

    try
    {

        response = RetryPolicy.Default.Retry<Exception, Collection<DriveItem>>(
        this.maxRetryCount,
        RetryDurationStrategies.ExponentialBackoff,
        (exception, timeSpan) =>
        {
            retryCount++;
            this.LogInfo(Strings.RetryPolicyMessage, exception.Message, timeSpan);
        },
        () =>
        {
            var driveItems = new List<DriveItem>();
            var driveItemsPage = this.GraphServiceClient.GetItemAndChildrenTask(id, username);
            var result = driveItemsPage.Result;
            driveItems.AddRange(result.Children.CurrentPage);

            while (result.Children.NextPageRequest != null)
            {
                var nextPageResult = result.Children.NextPageRequest.GetAsync();
                var nextPage = nextPageResult.Result;
                driveItems.AddRange(nextPage.CurrentPage);
            }

            Collection<DriveItem> driveItemsList = new Collection<DriveItem>(driveItems);

            return driveItemsList;
        });
    }
    catch (ServiceException ex)
    {
        switch (ex.Error.Code)
        {
            case ItemNotFound:
                this.LogWarning(ex, Strings.OneDriveItemNotFound, ex.Message);
                break;
            case InvalidRequestItemId:
                this.LogWarning(ex, Strings.OneDriveUserNotFound, ex.Message);
                break;
            default:
                this.LogWarning(ex, Strings.OneDriveGenericException, ex.Message);
                break;
        }
    }
    catch (AdalServiceException ex)
    {
        switch (ex.ErrorCode)
        {
            case InvalidClient:
                this.LogWarning(ex, Strings.AdalInvalidClientErrorMsg, ex.Message);
                break;
            case UnauthorizedClient:
                this.LogWarning(ex, Strings.AdalUnauthorizedClientErrorMsg, ex.Message);
                break;
            case InvalidRequestSecret:
                this.LogWarning(ex, Strings.AdalInvalidRequestErrorMsg, ex.Message);
                break;
            default:
                this.LogWarning(ex, Strings.AdalGenericErrorMsg, ex.Message);
                break;
        }

        throw new PluginException(string.Format(Strings.AuthenticationToOneDriveWasUnsuccessful, ex.Message), ex.InnerException);
    }
    catch (Exception ex)
    {
        this.LogWarning(ex, Strings.OneDriveGenericException, ex.Message);

        throw new PluginException(Strings.GenericPluginException, ex.InnerException);
    }

    return response;
}

Microsoft Graph SDK的调用:

public Task<DriveItem> GetItemAndChildrenTask(string id, string username)
{
    return this.GraphServiceClient.Drives[username].Items[id].Request().Expand(ExpandValue).GetAsync();
}

以下是result变量的屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:1)

您的底层Graph请求需要专门针对子节点进行,以使NextPageRequest不为空。

graphServiceClient.Drives[username].Items[id].Children.Request().GetAsync();

如果您执行以下操作:

graphServiceClient.Drives[username].Items[id].Request().Expand('children').GetAsync();

然后不会填充NextPageRequest。