Power Apps: A Deep Dive into the CRUD operations with Collections

Introduction

Collections are special types of variables or data storage mechanisms that can be used to manipulate the data within Power Apps. Collections are usually a group of similar items which act as a table of values with columns and rows. We can create, update, delete, use it as the data source for a gallery to view the items within Power App.

In this article, we will see how we can work with collections and explore the entire CRUD(Create, Read, Update, Delete)Lifecycle of the Collection within the Power App.

Add to Collection

So as to implement the addition of data to the collection, we will add the below 4 labels and text input using which user can input the data that needs to be added to the collection.

Labels

Text Inputs

Label_Manufacturer

TextInput_Manufacturer

Label_Model

TextInput_Model

Label_Month

TextInput_SalesMonth

Label_SalesCount

TextInput_SalesCount

We will also add a button and set the below formula on its OnSelect Property to add the Text Input Values to the Collection

Collect(SalesData,{Manufacturer:TextInput_Manufacturer.Text,Model:TextInput_Model.Text,Month:TextInput_SalesMonth.Text,Count:TextInput_SalesCount.Text})

Here we use the Collect method to save the data to the Collection named SalesData. The collection will be created with this name if it does not exist already. The key-value pair inside the braces are read as {Column in Collection: Value to be saved inside the colum}. The column will be created in the collection if it is not already present. So in this case it will create the column Manufacturer and save the text value from the input TextInput_Manufacturer . Same way, it will copy over the remaining input text values as well to the collection.

Graphical user interface, application, Word

Description automatically generated

Testing the addition of data to collection

Let’s click on the Preview button in the top right corner to play the app and test the data input. Adding the data and clicking on the button will save the input values to the collection

Graphical user interface, application

Description automatically generated

Heading back to the canvas and clicking on the View -> Collections will open up the collection in the UI which will list the recently added data.

Graphical user interface, application

Description automatically generated

Thus we can see the Collection named SalesData with the data

Graphical user interface, text, application

Description automatically generated

View the Collection Data

Since in the previous section, while clicking on the Button added the data to the collection in the back end and we couldn’t see the action in real-time, let’s add a Gallery control to view the added collection data as soon as it’s saved to the collection.

Let’s add a Gallery control and set its data source to SalesData Collection.

Graphical user interface, application

Description automatically generated

We can update the Gallery to show the desired layout by editing the Layout from the Properties tab. Below, we have changed the Image,Title, and Subtitle Layout to Title and Subtitle layout as we don’t have to show any image. We can also update the fields that need to be shown in the gallery by mapping the fields from collection to the Title and Subtitle in the layout.

Graphical user interface, application, Teams

Description automatically generated

As by default we have just 2 fields in the Title, Subtitle layout, we can add the remaining fields by dragging and dropping labels to the Gallery. Dropping the 2 new labels into the Gallery, will automatically pick the remaining fields in the collection and tag it to the new labels in the Gallery. In case you want to map the newly added labels to a different set of fields in the collection, we can do that by heading over to the Properties tab of the gallery ->Edit.

Graphical user interface, application

Description automatically generated

We have added a few Labels to denote the field names and rearranged the Gallery to look a bit more polished.

Graphical user interface

Description automatically generated

Since in our demo, we will be using the Gallery to write back updated values to the collection, having the label hold the values will make it impossible to write back. So as to make the values editable we will remove the Labels that hold the values of the collection and replace them with Text Inputs and retain the labels that hold the field names.

Graphical user interface

Description automatically generated

View Items in the Collection

Let’s test the View function of the Updated Gallery by playing the app. The newly added data is coming up live in the Gallery.

Graphical user interface

Description automatically generated

Edit the Individual Collection record

We will remove the Right arrow as we are not intending to show any child data on its click for now. Instead, we will add a new Button to Enable us to edit the individual records in the Gallery and Save them back to the Collection on Button Click.

But before doing that, let’s define a variable EditCollection which will have the initial value is false. We will use this variable to toggle the Edit Functionality of the Collection.

Graphical user interface, application

Description automatically generated

Next, we will add a button that lets us toggle the edit functionality of the collection. We will update the Variable to toggle the values using the formula :

Set(EditCollection,!EditCollection)
Graphical user interface, application

Description automatically generated

Now we will toggle the Display mode of the Value Text Input boxes based on the EditCollection variable value. So as to apply the formula to all 4 text inputs, let’s group them together.

Graphical user interface, application

Description automatically generated

We will then set the formula on the Group’s DisplayMode property to set it to Edit Mode if the Toggle Variable is true.

If(EditCollection = true,DisplayMode.Edit,DisplayMode.View)
Graphical user interface, application

Description automatically generated

Now that we have set the Functionality to toggle the Edit function of the collection, let’s add a Icon clicking on which it will save the changes back to the collection.

Patch(SalesData,Gallery_ShowSalesData.Selected,{Manufacturer:TextInput_Gallery_Manufaturer.Text,Model:TextInput_Gallery_Model.Text,Month:TextInput_Gallery_Month.Text,Count:TextInput_Gallery_Count.Text})

Let’s test the Save functionality by playing the app and changing the Count from 500 to 750

Graphical user interface, application

Description automatically generated

Clicking on Save will update the collection and we can see that the collection is now updated with the latest value of 750 for Polo GT

Graphical user interface

Description automatically generated

Remove Item from Collection

Let’s move to the final section of CRUD, where we implement the Row Wise Delete functionality as well as the entire collection deletion

Row Wise Deletion

So as to remove a row in the gallery from the collection, we will add a Trash Icon and add the below formula that will remove the record from the collection.

Remove(SalesData,ThisItem)
Graphical user interface, application

Description automatically generated

Delete Complete Collection

We can also add functionality to delete the entire collection in one go by adding a button and using the Clear function.

Clear(SalesData)
Graphical user interface, application

Description automatically generated

Running the app and clicking on Clear Collection has cleared the entire gallery

Graphical user interface, application

Description automatically generated

If we check from View->Collection, we can see that the collection has been cleared.

Graphical user interface, application

Description automatically generated

Summary

Thus we saw how to use Collection and work on the Complete LifeCycle of Create, Read, Update, Delete records within the Collection.

Related Articles

Author

Author

Priyaranjan KS is a Modern Workplace Architect primarily focused on developing and architecting solutions around Office 365,Power Platform and Azure.He is also a Microsoft Most Valuable Professional(MVP) and a Microsoft Certified Trainer(MCT)

Latest Articles