Type of fields in SharePoint with Patch in Power Apps

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.

Patch any type of field in SharePoint List Items using Power Apps

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.

9 thoughts on “Patch any field type in a SharePoint list item using Power Apps

  1. Thank you for this post, very interesting. What about updating a list entry? Like if you have the ID of the record?

  2. hello,I am trying to update a SP list by using the functions ForALll & Patch and thanks to a collection of item. The pb I have is the following: the process works fine when I am updating only 1 record. However if I need to update 2 records the 2 records are updated with the data coming from the second item in my collection.

  3. Hi,

    How would I use patch to set a person field of an existing record to blank?

    Background: I am making a digital asset register and need to make the Assigned field blank when an item is set “In Use = No”.

    I am trying to use Patch in the “OnChange” field of the “InUse” toggle.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: