I'm attempting to retrieve a list of group names from the Microsoft.Graph API using the following method
private async Task<Dictionary<string, string>> GetGroupNames(List<string> groupIds)
{
Microsoft.Graph.Groups.GetByIds.GetByIdsPostRequestBody requestBody = new()
{
Ids = groupIds,
// Why do I need to specify that the type is group when I'm using a constructor in the Groups namespace?
Types = new List<string> { "group" }
};
Microsoft.Graph.Groups.GetByIds.GetByIdsResponse result = (await graphServiceClient.Groups.GetByIds.PostAsync(requestBody))!;
Group[] groups = result.Value!.Cast<Group>().ToArray();
return groups.ToDictionary(g => g.Id!, g => g.DisplayName!);
}
result.Value
is then a list of DirectoryObjects where each object has ODataType
"#microsoft.graph.group".
From what I've seen on this website (e.g. here) it should be possible to simply cast the DirectoryObject
s to the correct type Microsoft.Graph.Group
, but that does not work. This gives the message
Unable to cast object of type 'Microsoft.Graph.Models.DirectoryObject' to type 'Microsoft.Graph.Models.Group'.
So how exactly am I meant to turn these DirectoryObjects into Groups?
Upgrading Microsoft.Graph
from 5.23.0 to 5.24.0 fixed it. I believe these are the relevant nuget packages
<PackageReference Include="Microsoft.Graph" Version="5.24.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.55.0" />
<PackageReference Include="Microsoft.Identity.Web" Version="2.13.3" />
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="2.13.3" />
I don't know if this was a bug in 5.23.0 or if I had incompatible nuget packages installed, but whatever the case it works now.
Edit: Thanks to @user2250152 I now know this was a bug in 5.23.0. See https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/2084