Microsoft Graph CSharp SDK - 获取 DriveItems - 分页不起作用,只能检索 1000 个项目中的 200 个项目
Microsoft Graph CSharp SDK - Fetching DriveItems- paging not working, only able to retrieve 200 items out of 1000 items
我正在针对 Microsoft Graph CSharp SDK
进行编码,同时测试我的大规模收集应用程序。我注意到我的代码存在分页问题。我将 1000 个 1kb 文件添加到一个测试 OneDrive 帐户 - 然后我 运行 我的代码反对看我是否可以访问所有文件。不幸的是,我只能在一个文件夹中看到 200 个文件 - 而不是完整的 1000 个文件。
我在网上查了一下,发现这个 GitHub(现已关闭)bug。
在单步执行我的代码时,我看到 NextPageRequest
是 null
。我只是想确保没有错误并确保我正确使用 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
变量的屏幕截图:
您的基础图形请求需要专门针对子项进行,以使 NextPageRequest 不为空。
graphServiceClient.Drives[username].Items[id].Children.Request().GetAsync();
如果你这样做:
graphServiceClient.Drives[username].Items[id].Request().Expand('children').GetAsync();
那么 NextPageRequest 将不会被填充。
我正在针对 Microsoft Graph CSharp SDK
进行编码,同时测试我的大规模收集应用程序。我注意到我的代码存在分页问题。我将 1000 个 1kb 文件添加到一个测试 OneDrive 帐户 - 然后我 运行 我的代码反对看我是否可以访问所有文件。不幸的是,我只能在一个文件夹中看到 200 个文件 - 而不是完整的 1000 个文件。
我在网上查了一下,发现这个 GitHub(现已关闭)bug。
在单步执行我的代码时,我看到 NextPageRequest
是 null
。我只是想确保没有错误并确保我正确使用 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
变量的屏幕截图:
您的基础图形请求需要专门针对子项进行,以使 NextPageRequest 不为空。
graphServiceClient.Drives[username].Items[id].Children.Request().GetAsync();
如果你这样做:
graphServiceClient.Drives[username].Items[id].Request().Expand('children').GetAsync();
那么 NextPageRequest 将不会被填充。