Is there any way to query to see if a user
exists in microsoft graph without an exception being thrown (i will be querying by id). For example i could do the following:
using Azure.Identity;
using System;
using System.Threading.Tasks;
public class GraphApiService
{
private readonly GraphServiceClient _graphServiceClient;
public GraphApiService(string tenantId, string clientId, string clientSecret)
{
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
_graphServiceClient = new GraphServiceClient(credential, new[] { "https://graph.microsoft.com/.default" });
}
public async Task<User> UserExists(string userId)
{
try
{
var user = await _graphServiceClient.Users[userId].Request().GetAsync();
return true;
}
catch (ServiceException ex)
{
Console.WriteLine($"Error getting user: {ex.Message}");
return false;
}
}
}
but I would much prefer a way that doesn’t throw an exception.
3
You can query the users endpoint and fitler on the userpricipalname then check the count of users returned eg
var users = await graphServiceClient.Users.Request().Filter("userPrincipalName eq '[email protected]'").GetAsync();
if (users.Count == 0)
{
}
1