Home > Software engineering >  check if graph.user manager exists without try catch
check if graph.user manager exists without try catch

Time:04-29

I have all my Active Directory users in a list and am iterating through the users to get their manager, using the below code.

  foreach (User u in userResult)
            {
            var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
            var directoryObject = await graphClient2.Users[u.UserPrincipalName].Manager
                .Request()  
                .GetAsync();
}

This works fine if the user has a manger, but if not, the code fails. The only way i can currently see around this is to use a try catch, but that feels crude, and when iterating over a lot of users, its not very efficient.

is there a better way of doing this?

CodePudding user response:

Get the user, not the manager. The user has a manager field which can be checked for null.

You still need to do try/catch in the case that the user is not a part of the org anymore or if the server has an internal error. 4xx and 5xx error responses throw exceptions in .NET code.

CodePudding user response:

You can use OData query enter image description here

  • Related