Click here to Skip to main content
15,884,237 members
Articles / All Topics

What is Collection & How to Manage a Collection in PowerApps ?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
28 Apr 2018CPOL3 min read 27.1K   1   1
What is collection & how to manage it in PowerApps?

In the last article, we learned about the different controls which exist in PowerApps although there are some more controls which we will discuss going forward. Before jumping into remaining controls, let’s discuss here the most important part Collection.

What is Collection?

A collection is used to store data that can be used in your app or we can say a collection is a group of items that are similar. For example, Student data collection where the collection will store student first name, last name and other information about the student.

To see it practically, let’s open PowerApps editor and add a new screen and OnStart function to create a collection.

Now to create a collection in PowerApps, we have  “Collection” function in PowerApp and syntax is as follows: 

C#
Collect(Collectionname, items)

So, Student collection can be defined as follows:

C#
Collect(StudentCollection,      {Name:"Rajat",LastName:"Jaiswal"},   {Name:"Sandeep",LastName:"Jain"})

OnStartCollectionNew

So, we have created our first collection. To view this in action, add any control you want. Here, we are using Gallery control.

AddGalControl

Once you added this control, you will find the data properties and we can see our created collection which is “StudentCollection”.

StudentCollection

Here, we can assign  the title and subtitle  properties of the gallery with the property Name & LastName of our new StudentCollection. Just save the application at this point and reload the app again.

You will find the following screen:

DataWithGallary

We can check our newly created collection by file option as shown below:

StudentCollection_001

In this way, we can create a collection. Now, the next question is how to ADD/EDIT/Remove in the collection. So, let’s understand this now.

Add Collection

Just drag-drop textboxes, labels, and a button to add the data to the collection as shown below. On Add button onSelect, write the below function:

addCollection

Now, when you run and click on Add button. You can see the Record appears in your gallery as shown in below figure:

Collection Add

Remove Collection

Now, in the next step, we will delete the record from the collection.

The syntax of this is very simple:

C#
Remove (CollectionName,item)

Now, to achieve this, add a delete icon in the gallery as shown in the below figure:

DeleteCollection

On select of delete icon, we can write Remove(StudentCollection, This.Item).

Here, This.Item is selected item of the gallery to which we need to delete from the collection.

Now, what if we want to delete the entire collection? Then in such case, we have:

Clear Collection

The syntax is very simple Clear(CollectionName)

So in our case, we have to write Clear(StudentCollection)

Now, to Edit a particular item in a collection, the first thing is to select the item in the text box which we want to edit and to achieve this, we bind the textbox by Gallery’s selected item as shown in the below figure:

Selectcollection

Once you are done with this, then on Update button, we can write Patch collection function as shown below:

PatchSection

So the syntax is very simple.

C#
Patch(StudentCollection,Gallery1.SelectedItem, {Name:TextInput4.Text,LastName:TextInput5.Text})

So in this way, we can update the specific item (as shown above, we updated only selected item from Gallery).

So, in this session, we understood Collection, add an item in the collection, Remove function to remove a specific item from the collection, Clear Collection to delete entire items from the collection and none other than Patch function to edit the collection.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
India India
I am Rajat Jaiswal from India. I am working as a Technology specialist in one of the reputed company in India with 12+ years of experience. I am a Microsoft Data Platform MVP & Alibaba Cloud MVP.
I have always had an affinity to learn something new in technology. Database, Web development, Cloud computing are the areas of my interests always.
I have been a regular contributor to technologies with more than 300+ tips. Learning & Sharing is one of my aims always. Writing blogs, delivering sessions, helping on forums is my hobby.

Comments and Discussions

 
QuestionHow to iterate in collection one by one? Pin
Member 135718615-Aug-18 22:35
Member 135718615-Aug-18 22:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.