Microsoft Office 365

Patch any field type in a SharePoint list item using Power Apps

In Power Apps the Patch function is very important when you work with data. In this post it’s all about how to update any field type in SharePoint lists.

Field types in SharePoint

In the past I looked at how to read and update fields in SharePoint using the REST API from Power Automate and today I noticed that I haven’t got a post about Power Apps on the same subject yet. In Power Apps a however i’m going to use the Patch function.

In this post I will look at updating the following type of fields:

  • Single line of text
  • Multiple lines of text
  • Choice
  • Number
  • Currency
  • Date and Time
  • Lookup
  • Yes/No
  • Person or Group
  • Hyperlink or Picture
  • Image
  • Task Outcome

Patch Syntax

First of all I will go through the syntax of the patch function. You can create new items or you can update existing items.

For new items you would typically use the following code

Patch(List, Defaults(List), {....})

For existing items you could use something like:

Patch(List, First(Filter(List, Title = "Whatever")), {...})

Or you could use the lookup function instead.

Patch(List, Lookup(List, Title = "Whatever"), {...})

In both cases you specify the list that you want to update. The items that you want to update, and the details of the update. The details of the update is what I want to focus on in this post.

For all of my examples below you could of course use variables to set the values of the field. Or you could refer to controls. So for example

{
   Title: TextInput1.Text
}

However to make the examples clearer I’ve specified the actual values as texts or numbers.

Single line of text

This is the easiest option. Every list has a field with the field type single line of text available, Title. This field Title field I will use as an example.

Patch(
    List, 
    Defaults(List),
    {
        Title: "New Title Value"
    }
)


Multiple lines of text

Compared to the Single line of text you might thing well that is just the same:

Patch(
    List, 
    Defaults(List),
    {
        Title: "New Title Value", 
        MultipleLinesOfText: "Multiple Lines Of Text Value"
    }
)

But that is cheating! That is updating a multi line field with a single line of text!

Using the Char(10) you can add new lines in your text.

Patch(
    List, 
    Defaults(List),
    {
        Title: "New Title Value", 
        MultipleLinesOfText: "Multiple Lines" & Char(10) &"Of Text Value"
    }
)

I’m aware that the multi line of text field can have other configurations. e.g. you could configure it to use HTML or rich text, which will make these kind of things potentially easier different.

Choice

The choice field is the first one of the types that is not obvious.

Patch(List,
         Defaults(List),
        {
           Choice:   {'@odata.type':"#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
                        Id:Gallery1.Selected.ID,
                        Value:Gallery1.Selected.Title}
              }
         )

Especially that @odata.type part is a bit of an ugly! and guess what? You can now leave that odata.type part out of the code.

Patch(List,
         Defaults(List),
        {
           Choice:   {
                        Id:Gallery1.Selected.ID,
                        Value:Gallery1.Selected.Title}
              }
         )

Then the Id and the Value are things that you will have to get from your choice field. Each option will have a unique number.

Patch(List,
         Defaults(List),
        {
           StatusChoice:   {
                        Id:3,
                        Value:"Complete"}
              }
         )

Number

The number field is another easy one. Just supply the field name and the value of the number. Note that there are no double quotes around the number value.


Patch(
    List, 
    Defaults(List),
    {
        Title: "New Title Value",         
        Number: 1234

    }
)

Currency

The currency is the same as the number field type


Patch(
    List, 
    Defaults(List),
    {
        Title: "New Title Value",         
        Currency: 1234
.50
    }
)

Date and Time

For Date and time type of field, I want to give 3 examples. First to get a date and time set to the current time, I can use the now() function.

Patch(
    List, 
    Defaults(List),
    {
        Title: "New Title Value",                 
        DateAndTime: Now()
    }
)

Or I could use the date function and set it to today or any other date.

Patch(
    List, 
    Defaults(List),
    {
        Title: "New Title Value",                 
        DateAndTime: Date(2020, 1, 13)
    }
)

Or I can even use the following syntax to take a text representing a date and time and set my date and time field in SharePoint.

Patch(
    List, 
    Defaults(List),
    {
        Title: "New Title Value",                 
        DateAndTime: DateTimeValue("2020-1-13T15:16Z")
    }
)

Lookup

The lookup is actually the same as a Choice field.

Patch(List,
         Defaults(List),
        {
           Lookup:   {'@odata.type':"#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
                        Id:3,
                        Value:"Complete"}
              }
         )

And once again, you can leave out the odata.type part out of your code, making the patch look so much cleaner.

Patch(List,
         Defaults(List),
        {
           Lookup:   {
                        Id:3,
                        Value:"Complete"}
              }
         )

Yes/No

Yes no fields can have two values true or false. To set the fields of the type Yes No we can simply use true and false values or expressions that give a true or false value.

Patch(
    List, 
    Defaults(List),
    {
        Title: "New Title Value",                 
        YesNo: true
    }
)

Person or Group

The Person field is probably the most complicated type of field. This Fieldtype requires you to supply 6 bits of information.

Patch(
    List, 
    Defaults(List),
    {
      Title: "New Title Value",  
      Person: {
            Claims: cmbOwner.Selected.Claims,
            Department: cmbOwner.Selected.Department,
            DisplayName: cmbOwner.Selected.DisplayName,
            Email: cmbOwner.Selected.Email,
            JobTitle: cmbOwner.Selected.JobTitle,
            Picture: cmbOwner.Selected.Picture
        }
   }
)

If you for example have a user’s name or email address you could use the Office365Users connector to get the rest of the details. Typically the SearchUserV2 function/method will help you get the the rest of the user details such as Claims. then to get the Picture you could use UserPhotoV2 from the same connector.

However, since a few weeks it all got a bit easier, You now only have to supply the Claims, DisplayName and the email.

Patch(
    List, 
    Defaults(List),
    {
      Title: "New Title Value",  
      Person: {
            Claims: cmbOwner.Selected.Claims,
            DisplayName: cmbOwner.Selected.DisplayName,
            Email: cmbOwner.Selected.Email
        }
   }
)

If you need any help with any of the above types feel free to open a chat or leave a comment below.

Share
Pieter Veenstra

Business Applications Microsoft MVP working as the Head of Power Platform at Vantage 365. You can contact me using contact@sharepains.com

View Comments

Recent Posts

Copy and paste Scope steps in the new Power Automate Designer

One of the outstanding issues with the new Power Automate Designer is Copy and Paste…

2 days ago

Receive the available storage within your SharePoint Online tenant

Within the SharePoint admin centre there is that little detailed overview, telling you the available…

3 weeks ago

Options for Documenting Your Power Apps: Comments, Code, and Controls

Within Power Apps there are various ways to document your app. In this post I'm…

4 weeks ago

2 ways to duplicate SharePoint Lists to support your Power Apps

Recently I've been asked quite a few times to duplicate SharePoint lists as part of…

1 month ago

Update a Hyperlink Column in SharePoint with Power Automate

Today, I was asked about how to create a lookup to a document or item…

1 month ago

429 Errors reported by the trigger in your flow in Power Automate

This morning I was asked about 429 Errors in flows, But rather than 429 errors…

1 month ago