C
C#15mo ago
alex098304

❔ Using Microsoft Graph to verify users exist based on IEnumerable<Guid>

I am trying to work with Microsoft Graph to verify that each guid in some list is associated with a user in the graph. I have noticed that there is some sort of property graphClient.Users.GetByIds that I intuitively think I can use to at least return a list of Users somehow, but I have no idea how it’s supposed to work, and I can’t seem to find documentation on it online that is useful. Mostly I am just very confused about how this API is supposed to work at all, and to me it’s just very unintuitive. Right now the best implementation I can come up with is to call graphClient.Users[guid.ToString()].GetAsync() with a config specifying that I don’t want any data returned. I do this for every Guid in my list, and I check if an exception is thrown. This seems kind of dumb to me, but it’s the only thing I can find that actually works. To be clear, I am writing an async Task<bool> method that accepts an IEnumerable<Guid>. It should return false if any supplied Guid is not associated with a user Id in the graph, and true if every Guid is associated with a user. I feel like there has to be a more intelligent solution than what I am currently using. Any help would be appreciated.
10 Replies
Erroneous Fatality
This could help you: https://learn.microsoft.com/en-us/graph/api/user-list?view=graph-rest-1.0&tabs=http You can call List and use the $filter parameter to filter by ids, and $select parameter to also only return the id properties, to reduce network load. You could maybe also use the count method with $filter. You put your id collection into the $filter, say you want to count only users whose Id is in the $filter, and then in memory you just check if the count of Ids is equal to the result count from the request
alex098304
alex098304OP15mo ago
I’m reading through it and it seems like there might be a good answer here. I’m trying to figure out how to apply the filter in C# now.
Erroneous Fatality
I can't seem to find if the AD has filtering capability of property in (value1, value2). If not, you'd have to transform your list of Ids into a list of string commands and then join them for the filter. Something like this:
IEnumerable<Guid> ids;
IEnumerable<string> idEquations = ids.Select(id => $"c/id eq '{id}'");
string idsFilter = string.Join(" and ", idEquations);
string filter = $"identities/any(c:{idsFilter})";
IEnumerable<Guid> ids;
IEnumerable<string> idEquations = ids.Select(id => $"c/id eq '{id}'");
string idsFilter = string.Join(" and ", idEquations);
string filter = $"identities/any(c:{idsFilter})";
Then use that for the filter parameter, and for the select just use id. Then when you get your results back, you can compare the two collections of Ids and figure out which ones are not present in the AD. This is all ad-libbed, I've never used AD before now, I was just curious ><
alex098304
alex098304OP15mo ago
Yeah. I wish the documentation was better for this API. I’m so confused. Like. I still dont understand exactly what the datatype of graphClient.Users even is or how the [] operator works on it. Like, I initially thought it was like a dictionary, but I guess it isn’t. I’ll work on this later and let you know how it goes.
Erroneous Fatality
Good luck 😄 You've got C# example snippets in the documentation page (https://learn.microsoft.com/en-us/graph/api/user-list?view=graph-rest-1.0&tabs=csharp#tabpanel_2_csharp)
alex098304
alex098304OP15mo ago
Use the filter query parameter to filter a collection of objects - ...
Learn how to use the filter OData query parameter and its operators against different types of properties in Microsoft Graph.
alex098304
alex098304OP15mo ago
Thanks for your direction by the way. I saw that page you posted earlier today, but I dismissed it until you pointed it out again. Thanks.
HtmlCompiler
HtmlCompiler15mo ago
when i have to deal with graph i look at net client with ilspy and compare it with docs and powershell client to try understand something and especially i try with graph explorer to verify all the stuff there are a discord group and a microsoft forum on this subject but usually they are not very active/useful
Erroneous Fatality
Glad to have helped 🙂
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?