I am using C# and the Graph Client to access data in the following endpoint.
var configAssignments = await graphClient.DeviceManagement.DeviceConfigurations[$"{a.Id}"].Assignments.GetAsync()
The response from graph is something like this:
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#deviceManagement/deviceConfigurations('%7B6a58caff-2b72-4fd1-ab9a-609af9e3b619%7D')/assignments",
"@microsoft.graph.tips": "Use $select to choose only the properties your app needs, as this can lead to performance improvements. For example: GET deviceManagement/deviceConfigurations('<{6a58caff-2b72-4fd1-ab9a-609af9e3b619}>')/assignments?$select=target",
"value": [
{
"id": "6a58caff-2b72-4fd1-ab9a-609af9e3b619_2c9626f7-aa6e-4629-84dc-779cd3dc8b89",
"target": {
"@odata.type": "#microsoft.graph.groupAssignmentTarget",
"groupId": "2c9626f7-aa6e-4629-84dc-779cd3dc8b89"
}
},
{
"id": "6a58caff-2b72-4fd1-ab9a-609af9e3b619_5a717ea1-f41a-415e-93ca-efcb845b2f99",
"target": {
"@odata.type": "#microsoft.graph.groupAssignmentTarget",
"groupId": "5a717ea1-f41a-415e-93ca-efcb845b2f99"
}
}
]
}
What I am trying to do is access the groupId within the target but I'm have trouble doing so. I can access the id just fine though.
if (configAssignments.Value != null)
{
foreach (var item in configAssignments.Value)
{
Assignment assignment = new Assignment();
assignment.GroupID = item.target.groupId; // doesn't work
assignment.Id = item.id // does work
Config.Assignments.Add(assignment);
}
}
Check if item.Target
is GroupAssignmentTarget
and cast item.Target
to GroupAssignmentTarget
if (configAssignments.Value != null)
{
foreach (var item in configAssignments.Value)
{
if (item.Target is GroupAssignmentTarget groupAssignment)
{
Assignment assignment = new Assignment();
assignment.GroupID = groupAssignment.GroupId;
assignment.Id = item.Id;
Config.Assignments.Add(assignment);
}
}
}