To begin, we need to create our UI metadata. What is UI metadata? It is data that describes the UI. This could be UI text, the input type, min/max numbers for sliders, question/section numbers, combo box options and anything else that may be of value to you. The metadata comes from a data source in PowerApps. In this article, we’ll be using a SharePoint List but SQL, Azure, Excel Sheets, and other data sources will work too.
Note: It is important to be consistent when creating UI metadata because the Power App will be using this data in If statements and inconsistency will break the UI.
To begin, I’ve created a blank canvas app. Let’s import the UI metadata.
Now that the data source is added to the PowerApp, let’s add it to a collection in the OnVisible function of our screen.
Code from screenshot above:
Refresh('UI Metadata List');
ClearCollect(UIMetadataCollection, 'UI Metadata List');
Before we jump into creating the dynamic UI, we need to create a collection that will keep track of user input.
Code from screenshot above:
Clear(UserInputCollection);
ForAll(
UIMetadataCollection,
If(ThisRecord.ui_type <> "label" && ThisRecord.ui_type <> "button",
Collect(
UserInputCollection,
{
ui_text: ThisRecord.ui_text,
user_input: "",
ui_type: ThisRecord.ui_type,
min_number: ThisRecord.min_number,
max_number: ThisRecord.max_number
}
)
)
);
Summary: All the UI metadata (labels, control types, etc) live outside of the canvas app. This gives us the ability to add, edit, or remove UI elements dynamically without changing anything in PowerApps. In this section we’ve connected the data source to our PowerApp and we’ve created a collection that will keep track of all user input in our dynamic form.
Now it’s time to jump into the main reason you’re here, the dynamic UI. This is where the magic really happens. First and foremost, let’s create a flexible gallery.
2. Change the items property to UIMetadataCollection (the collection that holds the UI metadata imported from the data source) as shown below
Note: Click the pencil in the top left corner of the gallery to insert UI elements.
Note: If you don’t see any text show up, refresh your PowerApp screen (save your work first). Another option is to create a second screen, add a button, and use that button to navigate to the Dynamic Form Screen. We need to do this because the OnVisible function of our screen needs to be triggered.
Code from screenshot above:
If(ThisItem.ui_type = "text_input", true, false)
Code from screenshot above:
If(ThisItem.ui_type = "date_picker", true, false)
If(ThisItem.ui_type = "slider", true, false)
Code from screenshot above:
If(ThisItem.ui_type = "checkbox", true, false)
Code from screenshot above:
If(ThisItem.ui_type = "checkbox", false, true)
Code from screenshot above:
If(ThisItem.ui_type = "button", true, false)
Summary: You may be starting to see a pattern here. In this section we made certain elements visible if they matched the ui_type in the metadata table. I challenge you to follow the same steps for the rest of the items in your UI metadata table.
So now that the dynamic UI is built, we need to keep track of user input. This is where the UserInputCollection will be used. We’ll be using Lookup and Patch functions found in PowerApps. Let’s jump right in!
Code from screenshot above:
Patch(
UserInputCollection,
LookUp(
UserInputCollection,
ui_text = Label.Text
),
{
user_input: TextInput.Text
}
);
If(datePickerChange,
Set(datePickerChange, false);
Patch(
UserInputCollection,
LookUp(
UserInputCollection,
ui_text = Label.Text
),
{
user_input: Text(DatePicker.SelectedDate)
}
);
)
If(sliderChange,
Set(sliderChange, false);
Patch(
UserInputCollection,
LookUp(
UserInputCollection,
ui_text = Label.Text
),
{
user_input: Text(Slider.Value)
}
);
)
Patch(
UserInputCollection,
LookUp(
UserInputCollection,
ui_text = Label.Text
),
{
user_input: Text(Checkbox.Value)
}
);
Summary: We now keep track of all user input within the dynamic form. My speculation on why this works is that PowerApps keeps track of what gallery element the user is interacting with. As a result, we can use the element’s information to update our UserInputCollection by calling Patch and doing a LookUp using the Label.Text.
For our last section, navigate to the button we created earlier in the article.
2. When you are done creating the flow, go back to the PowerApp and select the flow as shown below
When the flow has been added, the last step is passing the UserInputCollection to the flow in JSON format.
PowerAppsbutton.Run(
JSON(
UserInputCollection,
IncludeBinaryData
)
);
Note: If you need additional help exporting user input, take a look at this article written by Reza Dorrani. One more note: You can export user data to something else besides JSON as shown in the article.
Now that we’re done, save your application and give it a spin! When you click submit, go to your Power Automate and see it run. You can also add, edit, or remove UI metadata and see the UI updated in the PowerApp (after a browser refresh or button navigation).
Here is a final walk-through of everything we’ve built:
Happy Coding and God Bless!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.