Add Office 365 users to your application
The Office 365 Users connection for Power Apps lets you access user profiles in your organization using your Office 365 account. You can perform various actions such as get your profile, get a user's profile, get a user's manager, or get direct reports.
You can display this information via labels in your app. You can display one function, multiple functions, or even combine different functions. For example, you can use this function to make the app sound more personal by greeting the current user with their display name; or by automatically populating certain data fields with their name and phone number.
Add a connection
The Office 365 Users data source is a Standard connector that can be used in Power Apps. To add this connector, select Add data from the command ribbon and enter 'Office 365 Users' in the search field. Select Office 365 Users from the list, and, in the dialog box that appears with your connection, select your Office 365 Users connection.
You may see a similar dialog box to the one below, if this is the first time you're adding this connector in this environment. Select Connect to add this connection.
User profile information
The user profile information is called through the Office 365 Users MyProfile action. Likewise, we can get information about other users by calling the SearchUser action. You have a few options to get user information in the canvas app. To get information about the user who is using the app, you can either use the function at the App OnStart or OnSelect of a button.
Store user profile information using App OnStart
Here we get the record containing user information we're looking for and save it as a global variable, making it easy to reuse it throughout the app. Since this is a record of information, we have access to any of the information existing in your Office 365 profile.
Select App, select the OnStart property and add the following formula.
Set(varUserInfo, Office365Users.MyProfileV2())
To populate our variable select the ellipsis next to App, and select Run OnStart.
With the global record variable varUserInfo now populated, you can call that information anywhere in the app. For example, in the Default property of a Text input control, you can add varUserInfo.displayName
.
The image below shows attributes that can be automatically populated in your app, saving the user time versus inputting them manually. You can add text input fields and display additional fields such as job title, department, street address, city, state and postal code.
The data gathered from the MyProfileV2 action includes metadata from the Office 365 User's profile for the user. Once accessed, it can be used anywhere in the app.
Control OnSelect
If you need to gather data about another user (someone other than the current user), you can use the SearchUser and UserProfile actions. Let's practice this by adding a ComboBox to a canvas app with the Office365Users data source.
Set the properties of your combo box as follows:
Property | Value |
---|---|
Items | Office365Users.SearchUserV2({searchTerm: Self.SearchText, isSearchTermRequired: false}).value |
DefaultSelectedItems | Office365Users.SearchUserV2({searchTerm:varUserInfo.displayName}).value |
DisplayFields | ["DisplayName"] |
IsSearchable | true |
SelectMultiple | false |
OnChange | Set(varSearchUserInfo, Office365Users.UserProfileV2(Self.Selected.Id)) |
The SearchUser action gathers similar account profile metadata as the MyProfile action. In the OnChange property of our control we use the UserProfileV2 function to get the metadata for the selected user by using the Id of the selected user from the SearchUserV2.
Note
You will only be able to see all of the data if it has been entered in for that account, else you'll see a default or a null result. For example, if there is no mobile phone input for that person's record, this action will not return any data.
Next, copy the controls you made for the MyProfile action and paste them below the combo box. Change the labels and Default settings of the pasted text input fields as follows:
Label | Text Input Default |
---|---|
Full Name (Display Name) | varSearchUserInfo.displayName |
Job Title | varSearchUserInfo.jobTitle |
Department | varSearchUserInfo.department |
varSearchUserInfo.mail | |
City | varSearchUserInfo.city |
Country | varSearchUserInfo.country |
Postal Code | varSearchUserInfo.postalCode |
Press and hold the Alt key and select your combo box control, and you notice that it shows you the Display Names of available users as well as a search field. Go ahead and select a user and make sure that the text input fields below populate.
You can put your app into 'Play' mode and try searching for different users. Now that you've seen how we can get information for other users, let's look at some other action data available with the Office 365 Users connector.
Manager information
In addition to getting user information, you can get the user's Manager information.
With the ComboBox control you created, modify the OnChange property to: Set(varSearchUserInfo, Office365Users.ManagerV2(Self.Selected.Id))
.
Note
If you select a user that has no manager, you will notice that your text input fields may show errors when your app is in edit mode. This is because Power Apps is returning Error data that no manager exists for this user. You can then use that information to display notifications to your users such as: 'This person does not have a manager'. The Errors function returns information about your error, without disrupting the user experience.
You can confirm this is the correct manager in the Microsoft 365 admin center under Active users. Selecting the user's Display name opens a pane on the right side of the screen where you can view this information.
View all direct reports
Similar to finding Manager information, your Office 365 Users connector also lets you find all the users who directly report to a single manager.
We use a combination of a Collection and Gallery to present a list of Direct Reports. A Collection is a data variable that can store a list of data, or a table that is usable throughout your app. A Gallery control shows a table of records, and each record can contain multiple types of data. In this scenario, we establish a Collection as our data source. Begin by creating a new screen.
Add a ComboBox control and set the following properties:
Property | Value |
---|---|
Items | Office365Users.SearchUserV2({searchTerm: Self.SearchText, isSearchTermRequired: false}).value |
DefaultSelectedItems | Office365Users.SearchUserV2({searchTerm:varUserInfo.displayName}).value |
DisplayFields | ["DisplayName"] |
IsSearchable | true |
SelectMultiple | false |
OnChange | ClearCollect(colDirectReports, Office365Users.DirectReportsV2(Self.Selected.Id).value) |
Now let's present two pieces of information; one is to see how many users directly report to a manager, and the other is a list of their names.
To get a count of how many users report to a selected manager, add a Label control and set its Text property to CountRows(colDirectReports)
. This formula counts how many rows are in our collection of direct reports for the selected person. If you see a zero, then this person has no direct reports.
To display a list all the direct reports for thee selected person, insert a Vertical gallery and set the data source (or Items property) to colDirectReports. Change the Layout to Title and the Fields (or the Text property of the Title label in your gallery) to show displayName. When complete, your screen should look similar to this:
We've now experienced how to call action-based data via the Office 365 Users Connection. We've used this data to populate Text Input fields and populate another form of data called a Collection and show that information in a Gallery. In the next unit, let's explore further how to display and interact with data in a gallery.
For more information on the Office 365 Users connection, see Office 365 Users Connector reference.