A few days ago, I developed a leave management application for Power Apps. In it, employee leave requests undergo two-level approvals, so I created a dashboard for approvers to approve or reject single and multiple requests at a time with the help of the Power Apps Patch function.
In this article, I will explain how to patch a single gallery item in Power Apps and show various examples of how to patch all gallery items in Power Apps.
Power Apps Patch Gallery Selected Item [Single Item Only]
In my leave management application, the employee leave requests undergo two levels of approval. So, I displayed all employee leave requests in the Power Apps gallery control in the dashboard.
When approver 1 selects a pending leave request, he can provide comments and approve or reject it by clicking on the buttons. If he approves, the status will be updated as Approver 2 Pending; if he rejects it, it will be updated as Approver 1 Rejected.
When approver 2 approves the leave request, the status value is updated as Approver 2 Approved. If he rejects it, it will be updated as Approver2 Rejected. Approver2 can also provide comments that will be saved to the SharePoint list. The example below displays the same thing.
Here is the SharePoint list that contains employee leave requests.
Column Name | Data Type |
---|---|
Leave Title | Single line of text |
Employee Name | Person |
Leave Type | Choice(Sick ,Maternity, Annual, Compassionate) |
Start Date | Date & Time |
End Date | Date & Time |
Department | Choice(IT,Sales,Marketing,HR) |
Leave Days | Number |
Approver Comments | Multiline text |
Manager Name | Single line of text |
Status | Choice(Approver1 Pending,Approver1 Approved,Approver1 Rejected,Approver2 Pending,Approver2 Approved,Approver3 Rejected) |
1. In Power Apps, add a gallery control and provide the SharePoint list name below in its Items property.
'Employee Leave Requests'
2. In the gallery, to differentiate the status value of each leave request, add the below code in the Fill property of the status text label. Also, if you’re unaware of getting status value in the gallery, add the below code in its Text property.
Text = ThisItem.Status.Value
Fill = If(
ThisItem.Status.Value = "Approver1 Pending",
RGBA(174, 208, 221, 1),
ThisItem.Status.Value = "Approver1 Rejected",
RGBA(233, 79, 76, 0.83),
ThisItem.Status.Value = "Approver2 Pending",
RGBA(109, 49, 162, 0.68),
ThisItem.Status.Value = "Approver2 Approved",
RGBA(72, 163, 62, 0.9),
ThisItem.Status.Value = "Approver2 Rejected",
RGBA(233, 79, 76, 0.83)
)
Based on status values, the proved color will be applied to the status text label in the gallery.
3. Add a Power Apps form control and provide the codes for its properties given below.
DataSource ='Employee Leave Requests'
Item = Gal_EmpLeaveRequest.Selected
To display the gallery-selected item in the form control, we need to pass the above code in the item property of the form control.
- Gal_EmpLeaveRequest = Gallery name.
- ‘Employee Leave Requests’ = SharePoint list name.
Note: Except for the approver comments data card, all remaining Display Modes of the data cards are as View.
4. Add two button controls for approve and reject. Then, add the codes below to its OnSelect property.
Approve button’s OnSelect property:
If(
Gal_EmpLeaveRequest.Selected.Status.Value = "Approver1 Pending",
Patch(
'Employee Leave Requests',
Gal_EmpLeaveRequest.Selected,
{
Status: {Value: "Approver2 Pending"},
'Approver Comments': DataCardValue18.Text
}
),
Gal_EmpLeaveRequest.Selected.Status.Value = "Approver2 Pending",
Patch(
'Employee Leave Requests',
Gal_EmpLeaveRequest.Selected,
{
Status: {Value: "Approver2 Approved"},
'Approver Comments': DataCardValue18.Text
}
)
)
Here,
- Gal_EmpLeaveRequest = Gallery name.
- ‘Employee Leave Requests’ = SharePoint list name.
- DataCardValue18 = Approver comments data card value name in the form control.
- If the status value is “Approver1 Pending,” it will update as “Approver2 Pending.” Also, comments will updated.
- If the status value is “Approver2 Pending,” it will update as “Approver2 Approved”, comments are also updated.
Reject button’s OnSelect property:
If(
Gal_EmpLeaveRequest.Selected.Status.Value = "Approver1 Pending",
Patch(
'Employee Leave Requests',
Gal_EmpLeaveRequest.Selected,
{
Status: {Value: "Approver1 Rejected"},
'Approver Comments': DataCardValue18.Text
}
),
Gal_EmpLeaveRequest.Selected.Status.Value = "Approver2 Pending",
Patch(
'Employee Leave Requests',
Gal_EmpLeaveRequest.Selected,
{
Status: {Value: "Approver2 Rejected"},
'Approver Comments': DataCardValue18.Text
}
)
)
This code will update the status value like in the below conditions:
- If the status value is “Approver1 Pending, ” it will update as “Approver1 Rejected.” Also, the comments will be updated.
- If the status value is “Approver2 Pending,” it will update to “Approver2 Rejected.” Comments will also be updated.
Save the changes and preview the application. When you click the approve or reject buttons, the status values in the Power Apps gallery are updated based on the conditions I explained in the introduction.
This way, we can patch the selected item in the Power Apps gallery. Let’s see how to patch multiple items in Power Apps gallery control.
Patch All Gallery Items in Power Apps
With the above example, we’ll also try how to patch multiple Power Apps gallery items. In the example below, you can see that when I click on the Select all check box, the items in the gallery are selected, and when I click on the Approve button based on the status value, it is updated. This is also true for the reject button.
Follow the steps below to achieve this!
1. Add a checkbox control to the Power Apps gallery control. Then, provide the formulas below for the provided properties. Remove the text property value.
OnCheck =Collect(colItems,ThisItem)
OnUnCheck = RemoveIf(colItems,ID=ThisItem.ID)
Here, we’re creating a Power Apps collection named colItems to store gallery-selected items. The Removeif function removes the unchecked item in the gallery.
2. Add another Power Apps check box control and provide the formulas below for the provided properties.
OnCheck = Collect(colItems,'Employee Leave Requests')
OnUnCheck = Clear(colItems);
Text = "Select All"
To get the details directly from the SharePoint list, we’re creating a collection again in the OnCheck property of the select all check box.
When we uncheck the select all check box, the data present in the collection is cleared.
3. Now provide the below formulas for approve and reject buttons in its OnSelect property.
ForAll(colItems,
Switch(
true,
ThisRecord.Status.Value = "Approver1 Pending",
Patch(
'Employee Leave Requests',
ThisRecord,
{
Status: {Value: "Approver2 Pending"},
'Approver Comments': DCV_ApproverComments.Text
}
),
ThisRecord.Status.Value = "Approver2 Pending",
Patch(
'Employee Leave Requests',
ThisRecord,
{
Status: {Value: "Approver2 Approved"},
'Approver Comments': DCV_ApproverComments.Text
}
)
)
)
Here, the ForAll function took each record in the collection based on its status value and updated the next status.
Reject buttons OnSelect property:
ForAll(colItems,
Switch(
true,
ThisRecord.Status.Value = "Approver1 Pending",
Patch(
'Employee Leave Requests',
ThisRecord,
{
Status: {Value: "Approver1 Rejected"},
'Approver Comments': DCV_ApproverComments.Text
}
),
ThisRecord.Status.Value = "Approver2 Pending",
Patch(
'Employee Leave Requests',
ThisRecord,
{
Status: {Value: "Approver2 Rejected"},
'Approver Comments': DCV_ApproverComments.Text
}
)
)
)
Based on the status value, it will update each record in the Power Apps collection, and the status will be updated.
Save the changes and preview the app once. Click on the select all check box, which will select all items in the gallery. Then click the approve or reject buttons, which will update the status, as seen in the gallery itself.
I hope you understand the patch Power Apps gallery selected single and multiple items. In this article, I explained the Power Apps patch function and how to simultaneously patch gallery-selected items and bulk items.
Also, you may like:
- Power Apps sort gallery by person column
- Power Apps sort gallery by ID
- Add a new row in Power Apps gallery control
- Count rows in Power Apps gallery control
- Download a File from Power Apps
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com