locked
How to get list of Azure Active Directory users? RRS feed

  • Question

  • User-1308344544 posted

    the following method will return all users whose display name starts with 'abc'

     var users =  await graphClient.Users.Request().Filter("startsWith(displayName, 'abc')").GetAsync();

    once I get users I want to save this information to db.   Since this is async method, I might call save to database 
    method before I received list of users.  Is there any way to get Azure user list not using async method?  
    I know there are tricks that might resolve this issue using async method , for instance I can execute something like

                    using (var task = Task.Run(async () => await graphClient.Users.Request().Filter("startsWith(displayName, 'abc')").GetAsync()))
                    {
                        while (!task.IsCompleted)
                            Thread.Sleep(200);

                        res = task.Result;
                    }

    but this solution is not recommended.

    Wednesday, October 28, 2020 7:14 PM

Answers

  • User753101303 posted

    It likely comes from something else. You really tested just it line or could ie be something else you didn't show?

    Edit: I would start maybe with something such as :

    var task =  graphClient.Users.Request().Filter("startsWith(displayName, 'abc')").GetAsync();
    var result= await task; // the purpose of await is to run the task to completion (maybe cancelled or failed but not currently running any more)
    // tracing task.IsCompleted here should show it is always true
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 28, 2020 9:50 PM

All replies

  • User753101303 posted

    Hi,

    No. This an async method but the purpose of await is precisely to efficiently wait for completion (and more generally to make writing async code very close from writing sync code).
    If I remember the result might be paginated whiich is why it may not return all users in one go if this is what you see.

    If it doesn't help explain first which problem you see. 

    Wednesday, October 28, 2020 8:49 PM
  • User-1308344544 posted

    No, problem is not the number of returned records but the delay that may happen when calling async method.  I my case I call this method from my Controller and I am looking for just one user.  Once it is retrieved, I want to insert it to the database.  But I noticed there is delay and sometimes result may be empty.   The only way to overcome this that I found was to use sleep method - but this is not perfect solution.

    Wednesday, October 28, 2020 9:06 PM
  • User753101303 posted

    It likely comes from something else. You really tested just it line or could ie be something else you didn't show?

    Edit: I would start maybe with something such as :

    var task =  graphClient.Users.Request().Filter("startsWith(displayName, 'abc')").GetAsync();
    var result= await task; // the purpose of await is to run the task to completion (maybe cancelled or failed but not currently running any more)
    // tracing task.IsCompleted here should show it is always true
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 28, 2020 9:50 PM
  • User1686398519 posted

    Hi jstus, 

    You can use WhenAll: Creates a task that will complete when all of the Task objects in an enumerable collection have completed.

    I found an example here, you can check how to use it.

    • You can check this method GetUserDetailsAsync in the example.

    Best Regards,

    YihuiSun

    Thursday, October 29, 2020 6:21 AM
  • User-1308344544 posted

    so basically I have to use timer 

    Thank you for your help

    Thursday, October 29, 2020 8:44 PM
  • User753101303 posted

    You mean await ? It has nothing to do with a timer: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/await

    I'm starting to wonder if you have a real problem or just wrong expectations about what does async/await? This is the first time you are using this?

    Edit: or if you have a real problem, it is not caused by what you think. in this case you should really go back at trying to understand the exact problem. Do you use static data for later saving this to the db?

    Thursday, October 29, 2020 9:40 PM