Patch Function in PowerApps: A Complete Guide for Updating Data Seamlessly
Patch Function in PowerApps with SharePoint – Complete Guide
The Patch function in PowerApps with SharePoint helps app makers directly create and update records in SharePoint lists without relying only on forms. It provides flexibility to add new records, modify existing data, and even update multiple records at once. This makes it an essential function for building modern business applications that integrate with SharePoint.
What is the Patch Function in PowerApps with SharePoint?
The Patch function allows you to write data directly to SharePoint lists. Unlike SubmitForm, which requires a form control, Patch can be used with controls like text inputs, dropdowns, and galleries. With this flexibility, developers can manage scenarios where custom logic is needed to insert or update records.
Syntax of Patch Function
Patch( DataSource, BaseRecord, ChangeRecord1 [, ChangeRecord2, ...] )
- DataSource → The SharePoint list you are connected to.
- BaseRecord → The record you want to change, or
Defaults(DataSource)if you want to create a new one. - ChangeRecord(s) → The fields and values that need to be updated.
Adding a New Item to SharePoint
To create a new employee record in a SharePoint list named Employees:
Patch(
Employees,
Defaults(Employees),
{
Title: txtName.Text,
Department: drpDepartment.Selected.Value,
Salary: Value(txtSalary.Text)
}
)
This example creates a new row in the SharePoint list using values entered in text and dropdown controls.
Updating an Existing Item
To update fields in a record, you can use the SharePoint list’s ID column for accuracy:
Patch(
Employees,
LookUp(Employees, ID = Value(txtID.Text)),
{
Department: drpDepartment.Selected.Value,
Salary: Value(txtSalary.Text)
}
)
This will update the department and salary while leaving other columns unchanged.
🔹Create or Update (Upsert)
Sometimes you don’t know whether a record exists. You can combine If with IsBlank():
If(
IsBlank(LookUp(Employees, Title = txtName.Text)),
Patch(
Employees,
Defaults(Employees),
{ Title: txtName.Text, Department: "IT", Salary: 50000 }
),
Patch(
Employees,
LookUp(Employees, Title = txtName.Text),
{ Salary: 50000 }
)
)
👉 If the employee doesn’t exist → creates a new one.
👉 If it exists → updates the salary.
Using Patch with Galleries
When you display SharePoint data in a gallery, the selected item can be updated like this:
Patch(
Employees,
Gallery1.Selected,
{
Salary: Value(txtSalary.Text),
Department: drpDepartment.Selected.Value
}
)
This method is useful when users edit data directly from a gallery control.
Bulk Updates with Patch
If multiple records need updating, you can combine ForAll with Patch:
ForAll(
colUpdates,
Patch(
Employees,
LookUp(Employees, ID = colUpdates[@ID]),
{ Status: "Active" }
)
)
This approach applies the same change to multiple SharePoint items in one operation.
Working with SharePoint Column Types
Choice Columns
Patch(
Employees,
Defaults(Employees),
{
Title: "John",
Department: { Value: "HR" }
}
)
Lookup Columns
Patch(
Employees,
Defaults(Employees),
{
Title: "Jane",
Manager: { Id: 5, Value: "Michael Scott" }
}
)
People Columns
Patch(
Employees,
Defaults(Employees),
{
Title: "Alex",
AssignedTo: {
Claims: "i:0#.f|membership|alex@company.com",
DisplayName: "Alex Johnson",
Email: "alex@company.com"
}
}
)
Best Practices
- Use the ID column for precise updates.
- For new records, always use Defaults(SharePointList).
- Handle Choice and Lookup columns carefully with the right data structure.
- Validate inputs before patching to avoid errors.
- Minimize the use of bulk updates to prevent delegation issues.
Conclusion
The Patch function in PowerApps with SharePoint is a versatile way to manage data. It allows you to add, update, or bulk modify records without depending on forms. By mastering Patch, you gain the ability to build apps that are more flexible, efficient, and aligned with real-world business needs.