Search for Azure Active Directory users

Searching for Azure AD users can be challenging with the Search for users (V2) action. This blog shows how you could use the Graph API with a filter to workaround this in Power Automate.

Inspiration

This question from Ostritch52:

The Office 365 Users (Search for users (V2)) connector doesn’t seem to search on the employeeID field. Can this be done with Graph API?

Power Users Community thread: Search for AAD user, only knowing their Employee ID.

Limitation

As mentioned in the thread, the Search for users (V2) action is a bit limited. As the tooltip shows the search term will only be used for searching in fields like display name, given name, surname, mail, mail nickname and user principal name. Searching within fields like employeeID, department, city or mobilephone could be pretty useful.

searchforusers

Filter parameter

Luckily Ostritch52 is correct, you can search on employeeID via the Graph API. First of all you can use the List Users method. In addition to that you can use the OData filter query parameter in the Microsoft Graph API to specifically filter on EmployeeID.

filter_employeeid

To demonstrate this I have created a Power Virtual Agent which allows the user to search for a keyword and specify in which field of the Active Directory User profile they want to search for this keyword.

The flow steps

First we will create the Power Automate flow.

Before you start: this setup does require the Invoke an HTTP request action which is part of the premium HTTP with Azure AD connector. You might need an additional (trial) license in your tenant in order to be able to set this up.

pva_setup_searchaaduser

1. Create a flow with a When Power Virtual Agents calls a flow trigger action. Add a text AADAttribute input field and a SearchKeyword text input field to that action.

pva_trigger

2. Add an Invoke an HTTP request action. Use the GET method. Use the following Url for the request.

invokehttprequest

3. Add a Initialize Variable action of type string. Use Results as the name. Leave the value empty.

results_variable

4. Add a Condition action. Use the expression below to check if the results from the Invoke HTTP request are empty.

condition_results

5. In the If Yes, add a Set Variable action. Select the Results variable in the name. Use a text like ‘No employee found’ in the value field.

setvariable_noresults

6. In the If No, add a Select action. Use the value field of the Invoke an HTTP request action.

select_results

7. In the If No, add a Compose action. Rename it to Mark

markdowntable

8. In the If No, add another Set Variable action.

results_variable_results

9. Add a Return value(s) to Power Virtual Agents action. Add an output variable, call it Results. In the value use the Results variable of the flow.

returnvalue_pva_results

The bot steps

After you are done with the Power Automate flow setup you can continue building the topic in your Power Virtual Agent.

pva_colleaguesearch

1. Create a new blank topic.

topicaccounts

2. Give it a name, in this case ColleagueSearch. Add some trigger phrases. Via these you can interact with your topic. Save the topic.

3. Go to the Authoring canvas of your topic. Add the first question, What is is your keyword?. Select User’s entire reponse in the Identify field. Store the answer in a variable called varSearchKeyword of type text.

whatisyourkeyword

4. Add another question, Which field do you want to search in? This time use the Multiple choice options for the question. I used mobilePhone, city and employeeID as options. Store the answer in a variable called VarSearchAttribute of type text.

whatisyourfield

5. Add a Condition. Check if the VarSearchAttribute has a value.

varhasvalue

6. Add a Call an action. Connect it to your earlier created flow. Make sure you map the variables of the questions to the input fields of the flow.

search_callaction

7. Add a message action. Use the Results variable in the body of the message to show the results which the bot has found to the user.

message_pva

Happy testing!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.