I'm trying to follow this C# example at this link which Iterate over all the messages. https://learn.microsoft.com/en-us/graph/sdks/paging?tabs=csharp
I have applied that paging to something similar to instead iterate through members that belong to a group and the code is throwing an error on the return true; line of all places. Can anyone help me understand why I get the error:
Error CS0029 Cannot implicitly convert type 'bool' to 'System.Threading.Tasks.Task'
Here is the code:
var members = await
graphClient.Groups[groupObjectId].Members.GetAsync(requestConfiguration =>
{
requestConfiguration.QueryParameters.Top = 999;
});
var pageIterator = PageIterator<Group, GroupCollectionResponse>
.CreatePageIterator(
graphClient,
members,
// Callback executed for each item in
// the collection
(m) =>
{
Console.WriteLine($"Group ID: {m.Id}");
Console.WriteLine($"Group Name: {m.DisplayName}");
Console.WriteLine($"Group Description: {m.Description}");
Console.WriteLine($"Group Created Date Time: {m.CreatedDateTime}");
//count++;
return true;
},
// Used to configure subsequent page
// requests
(req) =>
{
// Re-add the header to subsequent requests
req.Headers.Add("Prefer", "outlook.body-content-type=\"text\"");
return req;
}
);
await pageIterator.IterateAsync();
I think the issue is that the query returns DirectoryObjectCollectionResponse
not GroupCollectionResponse
. So, you need to modify the PageIterator
and for each item check if the item is either User
or Group
You don't need to configure subsequent page request (adding header req.Headers.Add("Prefer", "outlook.body-content-type=\"text\"")
makes sense only for messages as it is in the example)
var pageIterator = PageIterator<DirectoryObject, DirectoryObjectCollectionResponse>
.CreatePageIterator(
graphClient,
members,
// Callback executed for each item in
// the collection
(m) =>
{
if (m is User user)
{
Console.WriteLine($"User ID: {user.Id}");
Console.WriteLine($"User Name: {user.DisplayName}");
Console.WriteLine($"User Created Date Time: {user.CreatedDateTime}");
}
else if (m is Group group)
{
Console.WriteLine($"Group ID: {group.Id}");
Console.WriteLine($"Group Name: {group.DisplayName}");
Console.WriteLine($"Group Description: {group.Description}");
Console.WriteLine($"Group Created Date Time: {group.CreatedDateTime}");
}
//count++;
return true;
}
);
You can even cast the response to the group
var groups = await graphClient.Groups[groupObjectId].Members.GraphGroup.GetAsync(requestConfiguration =>
{
requestConfiguration.QueryParameters.Top = 999;
});
var pageIterator = PageIterator<Group, GroupCollectionResponse>
.CreatePageIterator(
graphClient,
groups,
// Callback executed for each item in
// the collection
(m) =>
{
Console.WriteLine($"Group ID: {m.Id}");
Console.WriteLine($"Group Name: {m.DisplayName}");
Console.WriteLine($"Group Description: {m.Description}");
Console.WriteLine($"Group Created Date Time: {m.CreatedDateTime}");
//count++;
return true;
});