Sometimes I have to do some Microsoft projects with the Power Platform and more especially with Power Apps. Because I am not a master with it, I spend a considerable time to search some answers πŸ˜’ I decided to note all the points about how to create and update SharePoint item fields through a Power Apps application.

Some notes before updating an item

During my Power Apps projects, I noted some behaviors that I did not expect... So, I noticed them here πŸ™‚.

Change the Control Type of a field

Change Control Type UI

When the Control type of a field is changed, the DataCardValue number change at the same time. If some functions use this DataCardValue before changing the Control Type, think to update the DataCardValue number in the function too.

[note]Note

It is not possible to change the "type" of the control (ex: text <> choice). The only way is to delete the existing field and replace it with the new one (if the type has changed on the SharePoint side)

Create an item

The first easiest method to create an item is to use a Form and the SubmitForm()

SubmitForm(myItemForm);
Submit the form to create a new item

This method is pretty easy to use and has the advantage to validate the form before sending the request. All fields that return an error will be highlighted with a red border and an error message. The validation of the form takes into account the configuration of the SharePoint list fields.

But, if the submit function is not performed directly on the screen that contains the form, the highlighted fields are not persistent.

Example: a form split into several screens; some fields are mandatory or have some constraints; the final screen performs the submit.

If the user returns to a previous screen to fix the error(s), the specific field(s) in error won't be highlighted.

[Success] Tip

To manage the fields with an error, use a Collection to store the name or internal name of each error field. Then, for each one of them, handle the highlight manually.

The other method consists to use Patch(). It is not possible to send directly a Form. Each field must be manually specified, and of course, the form won't validate before and you are faced with the same problem for the highlight error fields.

Patch(
    SharePointList,
    Defaults(SharePointList),
    {
        fieldOne: DataCardValue1.Text,
        fieldTwo: DataCardValue2.Text,
        ...
    }
);
Use Patch() to create a new item

It is possible to check if the form is valid:

If(myItemForm.Valid, "it is ok", "oups");
Check if the form is valid or not

or validate each field based on the data source constraints:

If(
    IsBlank(Validate(SharePointList, spFieldInternalName, DataCardValue.Text)),
    "it is ok",
    "oups"
);
Check field validation in accordance with the data source

Update an item

SubmitForm() still working to update an item. The prerequisites are:

  1. FormMode.Edit
  2. Item must be specified

It is possible to use Patch() also :

Patch(
    SharePointList,
    First(Filter(SharePointList, ID = 10)),
    {
        fieldOne: DataCardValue1.Text,
        fieldTwo: DataCardValue2.Text,
        ...
    }
);
Use Patch() to update an item

The Update() methods work in the same way (just replace Patch by Update). The big pro of this method is the Filter() and SharePoint list with more than 2000 items.

Another alternative is to use UpdateIf():

UpdateIf(
    SharePointList,   
    ID = 10,
    {
        fieldOne: DataCardValue1.Text,
        fieldTwo: DataCardValue2.Text,
        ...
    }
);
Basic usage of UpdateIf()

  • No filter condition
  • Easy to perform several conditions
  • Less code for several use cases
UpdateIf(
    SharePointList,   
    ID = 10 And 'Content Type'.Name = "CTOne",
    {
        fieldOne: DataCardValue1.Text
        ...
    },
    ID = 10 And 'Content Type'.Name = "CTTwo",
    {
        fieldTwo: DataCardValue2.Text,
        ...
    }
);
Use UpdateIf() with several conditions

It is so possible to create a new item from one form and update this new item with additional fields from another form for example:

SubmitForm(myItemForm);
If(
    // If the submit worked, the LastSubmit return the new item
    Not(IsEmpty(myItemForm.LastSubmit)),
    UpdateIf(
        SharePointList,   
        ID = myItemForm.LastSubmit.ID And 'Content Type'.Name = "CTOne",
        {
            fieldOne: DataCardValue1.Text
            ...
        },
        ID = myItemForm.LastSubmit.ID And 'Content Type'.Name = "CTTwo",
        {
            fieldTwo: DataCardValue2.Text,
            ...
        }
    ),
    false
);
Use SubmitForm() to create a new item and UpdateIf() to update it directly after creation

Now with this information in mind, let's see how to send data in accordance with the field type.

You may also be interested in