0

I am getting the the groups based on users from Microsoft Graph Api. Although I am getting the groups but they are coming to total count of 100.

I tried to use paging technique but it is keep failing. Can someone help me out?

 var page = graphClient
                   .Users[uniqueIdentification]
                    .MemberOf
                    .Request()
                    .GetAsync().Result;

        var names = new List<string>();

        names.AddRange(page
                .OfType<Group>()
                .Select(x => x.DisplayName)

        .Where(name => !string.IsNullOrEmpty(name)));

The above code only return top 100.

When I tried below code for paging it got cast error .

Error:

Unable to cast object of type 'System.Collections.Generic.List`1[Microsoft.Graph.DirectoryObject]' to type 'System.Collections.Generic.IEnumerable`1[Microsoft.Graph.Group]'.

Code:

    var group2 = new List<Group>();
        var groupsPage = graphClient.Users[uniqueIdentification].MemberOf.Request().Top(300).GetAsync().Result;
        group2.AddRange((IEnumerable<Group>)groupsPage.CurrentPage);
        while (groupsPage.NextPageRequest != null)
        {
            groupsPage =  groupsPage.NextPageRequest.GetAsync().Result;
            group2.AddRange((IEnumerable<Group>)groupsPage.CurrentPage);
        }
Jashvita
  • 553
  • 3
  • 24

1 Answers1

0

The maximum sizes of pages are 100 and 999 user objects respectively and are default .

Some queries are supported only when you use the ConsistencyLevel header set to eventual and $count to true

For the error:

Unable to cast object of type 'System.Collections.Generic.List`1[Microsoft.Graph.DirectoryObject]' to type 'System.Collections.Generic.IEnumerable`1[Microsoft.Graph.Group]'.

It looks like here group2 is of type list but trying to add/append the page of type Ienumerable to it which is leading to cast error.(note:list implements Ienumerable (parent) but ienumerable doesn't inherit from list as Ienumerable can be list, queue ,stack )

using System.Linq;

While using var group2 = new List<Group>();

try to add .ToList(); or .Cast<group>().ToList(); depending on latest or old type of versions before adding or appending to the group2 of type list

Please check the References to know further details :

  1. List users - Microsoft Graph v1.0 | Microsoft Docs
  2. How to append enumerable collection to an existing list in C# - Stack Overflow
kavyaS
  • 8,026
  • 1
  • 7
  • 19