Bulk Patch Made Simple: Updating Gallery Items in One Go
Learn how to use the lesser-known batch overload of the Patch function to write multiple SharePoint list items at once, drastically improving performance.
Working with large galleries often requires updating several records at once. The standard ForAll+Patch loop sends one request per row, which can feel slow when you have more than a handful of items. Power Apps includes a hidden gem—a batch overload of Patch—that lets you write all changes in a single call. This article walks through a realistic scenario and shows you how to apply the pattern in your own apps.
The Batch Pattern in a Nutshell
The single‑call version of Patch accepts three arguments:
- The data source (e.g., a SharePoint list).
- A table of primary‑key values (usually the
IDcolumn). - A table of matching records that contain only the columns you want to update.
Instead of looping, Power Apps bundles all updates into one network request, giving you a noticeable speed improvement.
Scenario: Product Price Adjustment App
You work for a retail company that uses a SharePoint list named ProductCatalog to track inventory. The list has these columns:
ID(auto‑generated)- Title – product name (single line of text)
- Price – current selling price (single line of text)
Staff need to review products in a gallery, edit the Price field for several items, and save all changes at once.
Sample Data
| ID | Title | Price |
|---|---|---|
| 1 | Widget A | 19.99 |
| 2 | Widget B | 24.99 |
| 3 | Widget C | 14.99 |
| 4 | Widget D | 29.99 |
Building the App
1. Add a Gallery and Connect the Data Source
Add the ProductCatalog datasource to your app. Insert a Vertical Gallery and set its Items property to:
ProductCatalog
2. Design the Gallery Template
Inside the gallery, add a Label to show the product name:
ThisItem.Title
Add a Text Input control where the user can edit the price. Set its Default property to the current value stored in the list:
ThisItem.Price
3. Create a Save Button
Below the gallery place a Save button. We will write all changes to SharePoint when the user clicks it.
The strategy:
- Build a collection of only the items whose price has actually changed.
- Compare the list value (converted to text to avoid null issues) with the current text input value.
- Use the batch
Patchto write the updates.
ClearCollect(
colPriceUpdates,
ForAll(
Filter(
galProducts.AllItems,
Text(Price) <> txtPrice.Text
),
{ ID: ThisRecord.ID, Price: txtPrice.Text }
)
);
Patch(
ProductCatalog,
colPriceUpdates.ID,
colPriceUpdates
);
Notify(
CountRows(colPriceUpdates) & " price(s) updated successfully",
NotificationType.Success
);
Reset(galProducts)Explanation:
ClearCollect(...colPriceUpdates...)creates a table of only the edited rows.Filter(galProducts.AllItems, Text(Price) <> txtPrice.Text)keeps rows where the text input differs from the SharePoint value.Text()convertsnullto an empty string so the comparison works reliably.Patch(ProductCatalog, colPriceUpdates.ID, colPriceUpdates)applies all updates in one call. The second argument is a table of IDs (each row has one columnID). The third argument is the table of matching records.- After the call the gallery is reset so it refreshes from the datasource.
4. Add Proper Error Handling
A real app should handle failures gracefully: check if any rows changed, catch backend errors, and show meaningful messages.
ClearCollect(
colPriceUpdates,
ForAll(
Filter(
galProducts.AllItems,
Text(Price) <> txtPrice.Text
),
{ ID: ThisRecord.ID, Price: txtPrice.Text }
)
);
If(
!IsEmpty(colPriceUpdates),
If(
IsError(
Patch(
ProductCatalog,
colPriceUpdates.ID,
colPriceUpdates
)
),
Notify("Update failed – please try again", NotificationType.Error),
Notify(
CountRows(colPriceUpdates) & " price(s) updated",
NotificationType.Success
)
),
Notify("No prices were changed", NotificationType.Error)
);
Reset(galProducts)5. Let Users Cancel Edits
Provide a Cancel button that reverts all text inputs back to the original list values. The trick is to toggle a variable that is bound to the Reset property of each text input.
Place a Cancel button beside the Save button. Set its OnSelect to:
Set(gblResetInputs, true); Set(gblResetInputs, false); Reset(galProducts)
Then, back in the gallery, select the text input and set its Reset property to:
gblResetInputs
When the cancel button is clicked the variable pulses true→false, which triggers a reset of the text input to its Default value (the original SharePoint price). The Reset(galProducts) call scrolls the gallery back to the top if any scrolling had occurred.
Performance and Delegation Notes
-
Why it’s faster: Writing N items with
ForAllsends N separate HTTP requests to SharePoint (or other datasource). The batchPatchsends a single request containing all changes. The saving becomes significant above 5–10 items. -
Delegation: The
FilterinsideClearCollectuses theAllItemsproperty of the gallery. When the datasource is SharePoint,AllItemsreturns all records (subject to the 500‑item gallery limit). If your list grows beyond 2000 items, consider usingFilterdirectly on the data source instead of onAllItemsto stay within delegation limits. -
Other datasources: This overload works with any datasource that supports updating multiple records in one operation (e.g., Dataverse, SharePoint). For SQL Server you may need a different approach because the current batch
Patchis not supported for SQL tables.
Common Pitfalls
| Mistake | Solution |
|---|---|
Forgetting Text() when comparing values | Wrap the SharePoint column with Text() to handle null. |
Omitting ID from the updates table | The batch Patch uses the second argument (IDs) to locate records. You can leave ID out of the third argument, but including it does no harm. |
Using $"{}" inside code that will be passed as a template literal | Use the & concatenation operator (e.g., "Updated " & CountRows(col) & " rows"). |
| Not resetting the gallery after save | Without Reset(), the outputs may still show the old values until a manual refresh. |
Final Recommendation
The batch overload of Patch is one of the simplest performance wins you can apply to a Power Apps gallery. It reduces network chatter, makes the UI feel snappier, and the code stays readable. Next time you need to write multiple edits, skip the ForAll and reach for the table‑based variant.
References
- Original article: Fastest Way To Patch All Gallery Items In Power Apps by Matthew Devaney
- Microsoft Learn: Patch function reference
Learn how small coding practices—like using descriptive names, flattening conditions, and simplifying logic—can make your apps easier to update and less error-prone.
Move past the gallery and discover the hidden patterns for validation, navigation, and smart submission handling in your data entry forms.
PowerShell unlocks admin capabilities that the Power Platform admin center simply doesn’t offer—from recovering deleted apps to blocking trial licenses. Here’s how to wield them safely.