Microsoft Graph。跟踪页码

时间:2018-06-15 12:17:39

标签: c# microsoft-graph

我正在实现一种方法,从AzureAD中获取组,并对组的displayName属性进行分页和过滤。让我们在分页中居中。我实际上能够使用$top执行页面结果。但我的要求是说我需要从特定页面返回数据集。我也能够做到这一点,但是没有一个非常优雅的解决方案,请求结果的NextPageRequest对象检索指定的页面。这是代码:

public async Task<List<Group>> GetGroups(string filterText, int page, int pageSize = 1)
{
  var graphClient = _graphSdkHelper.GetAuthenticatedClient();
  List<Group> groups = new List<Group>();

  //Microsoft Graph api allows a minimum page size of 1 and maximum of 999
  if (pageSize < 1 || pageSize > 999)
  {
    return groups;
  }

  try
  {
    IGraphServiceGroupsCollectionPage graphGroups;
    if (!string.IsNullOrEmpty(filterText))
    {
       graphGroups = await graphClient.Groups.Request().Filter($"startswith(displayName, '{filterText}')").Top(pageSize).GetAsync();
    }
    else
    {
      //if filter text is empty, return all groups
      graphGroups = await graphClient.Groups.Request().OrderBy("displayName").Top(pageSize).GetAsync();
    }

    //navigate to the requested page. This is extremly inefficient as we make requests until we find the right page.
    //$Skip query parameter doesn't work in groups services
    var currentPage = 1;
    while (currentPage < page && graphGroups.NextPageRequest != null && (graphGroups = await graphGroups.NextPageRequest.GetAsync()).Count > 0)
    {
      currentPage = currentPage + 1;
    }

    foreach (var graphGroup in graphGroups)
    {
      Group group = _graphSdkHelper.TranslateGroup(graphGroup);
      groups.Add(group);
    }
  }
  catch (Exception exception)
  {
    _logger.LogError(exception, "Error while searching for groups");
  }

  return groups;
}

问题1:如何改进和跟踪我的哪个页面?这可能吗?现在我可以请求下一页,所以如果我需要访问第20页,例如,意味着20个azure请求。

关于AzureAD Graph Api中的这篇文章,这是不可能的:  https://social.msdn.microsoft.com/Forums/en-US/199bbf92-642a-4bcc-add4-f8023a7684e2/paging-in-azure-ad-graph-client?forum=WindowsAzureAD

我只是拒绝认为在AzureAD中进行真正的分页是不可能的。

问题2:如何只用一个请求获得组的总数。似乎没有为组或用户实现$Count查询参数:

https://github.com/microsoftgraph/microsoft-graph-docs/blob/master/concepts/query_parameters.md#count-parameter

由于

0 个答案:

没有答案