Continuing my series on building a modern SharePoint solution using PowerApps and Flow I want to show how to go about setting a SharePoint people picker field programmatically in PowerApps. This goes back to one of the requirements listed for my solution in part 1 of this series: “Requestor’s manager should be auto-populated as an approver”. What’s nice about this is the manager field is filled in already by the company’s administration system. It exists in Azure AD and thus within Office 365, which just so happens to have a connector that easily allows us to build with it. I am getting ahead of myself though. Let’s dig in and learn how to do this.
Posts in this series:
- Preparing SharePoint
- Integrating PowerApps as a Custom List Form
- Customizations All List Form PowerApps Should Have
- Setting a SharePoint People Picker Field Programmatically in PowerApps
- Cascading Drop-down Lists and People Picker in PowerApps
- Create a Flow to be Started Manually
- Creating a Multiple Approver Microsoft Flow
- Sending a Tweet from Microsoft Flow
- Starting a Microsoft Flow from a PowerApp
Adding a Connector to get User Data
The first thing we want to do is update our application with a connector that allows us to access data within the Office 365 environment. What’s nice (as I stated before) is this is controlled via Azure AD so a lot of the fields we need are populated for us including the manager field.
Special Note: this blog assumes that you have the information required filled in within your AD. If you don’t another method not covered today will be required.
Microsoft has provided the O365User connector that will allow us to gather the data we need. You can read up more on the connector here. To add the connector to your app perform the following steps:
- Within your app builder click on the View tab
- Click on Data sources
- Click on + Add data source in the window that slides out
- Scroll down until you find Office365Users and click on it.
- You may be prompted to login. Do so with an account that has access to at least view the accounts in O365 (most accounts should have read ability).
Add a Context Variable To Hold Manager Information
One thing we don’t want to do is create a form that continually polls SharePoint or Office 365 through the connector. So do avoid this we are going to load the manager information once via the form’s OnVisible event.
- Click on the screen object for your “New” activity.
- Modify the OnVisible event to create or modify a context variable to hold the manager’s information
- Add the following code to the OnVisible event:
- What this function is doing is creating or updating the approveMgr context variable. It is getting the manager based on the user who is logged in (MyProfile()).
- Next we need to edit the “Default” property of the People Picker object on the form.
- Very Important: Update the Data Card Value (drop down box) and not the Data Card itself. I made this mistake once and took me forever to figure out why it wasn’t updating.
- Add the following code to the Default property. The code tells the solution to use the SP User data type based on the manager’s account and to fill in the information the People Picker requires (department, email, display name and job title if they exist).
Next set the data card to read only (DisplayMode = View). Once you run the form with the above settings you will now see the People Picker auto fills the field with the manager’s information (and it will be a user object, not just text).
Thanks for reading!!