How to get the details of a newly created SharePoint item with SubmitForm(), Patch() and Collect()

Writing data to SharePoint can be done in multiple ways: by using a Form or submitting the contents of one or multiple controls using the Patch() or Collect() functions. But how do you proceed with your item after that?

In this blogpost, I will explain how you will get the details of your newly created SharePoint item from within your Power App.

SubmitForm()

The most straightforward way to write data to SharePoint is using the Form control. It allows you to directly connect a SharePoint data source to it and it has multiple modes to display the data:

  • New
    Allows you to create new items. After an item is created, it will automatically change to Edit mode, but it doesn’t know which item to open. I will explain how to get the item details later on.
  • Edit
    Allows you to edit existing items (using the Item property of your Form).
  • View
    Allows you to view existing items (using the Item property of your Form).

Form types

When adding a Form control to your screen, you have the option of 2 types:

  • Edit
  • Display

I always use the Edit type, because that allows you to change to all of the Form modes (New, Edit, View) at any time. The Display type doesn’t give you that option, which makes it less flexible.

Getting your item details

After you’ve configured your form with all data cards, you add a button to your screen which allows you to submit the form to SharePoint. You can use the SubmitForm() function for this:

SubmitForm(Form1)

If nothing weird happened, your data should’ve been submitted to SharePoint after this. This will provide the form with a ‘new’ property that comes in handy for our use-case:

Form1.LastSubmit

This LastSubmit property contains all of the information of the last item that was send to SharePoint using the form. All we have to do is to write this information to a variable and we can use all of the data in our Power App:

Set(
    varSubmittedItem,
    Form1.LastSubmit
)

You can add this directly after your SubmitForm() function, but the Form control has another nice property called OnSuccess. With this property, you can define which actions you want to take place after an item has been successfully submitted to SharePoint. If you put your function in there, the variable will only be filled when the item has been created in SharePoint.

There is also a property called OnFailure, which allows you to do something when an item hasn’t been successfully submitted to SharePoint.

Now that you have your item details, you can set the Item property of your Form to varSubmittedItem. This make will make your Form load correctly in Edit mode after submitting. You can also extract certain fields of the item (e.g. ID or Title) and use it anywhere in your Power App:

You can also tell your Power App not to load the Form in Edit mode by adding the following function after your Set() function on the OnSuccess property:

ResetForm(Form1)

This will put the Form back into New mode, which allows you to enter new data.

Patch()

Another way to write data into SharePoint is by using the Patch() function. This function allows you to create or update items and required at least 3 parameters:

  • Source
    The data source to be patched
  • Record
    The record that has to be patched
  • Update
    The update that needs to be patched to the given record

When creating new items, we don’t want to provide a record to be updated. You can just leave the Record parameter blank, but that will not provide you with the Update fields that you specify in your app:

Patch(
    TestList,
    {},
    {Title: DataCardValue1.Text}
)

This will only return the value of the column(s) your specified in your Patch() function (in this case only the Title column). To make sure all item details are returned, you need to use the Defaults(<DataSource>) function:

Patch(
    TestList,
    Defaults(TestList),
    {Title: DataCardValue1.Text}
)

Now, every field can be returned. So if we want to get this information into a variable, we simply wrap a Set() function around the Patch() function:

Set(
    varPatchedItem,
    Patch(
        TestList,
        Defaults(TestList),
        {Title: DataCardValue1.Text}
    )
)

To access the details of the item, you can just call upon the varPatchedItem variable from within your Power App.

Collect()

With the Collect() function, you can also write data into SharePoint. It is basically intended for batch creating items, but you can also write a single item back to SharePoint with this function.
It requires at least 2 parameters:

  • Collection
    A new or existing collection to augment
  • Item
    A record or table to collect. A record will be appended to the collection. A table will have its rows appended to the collection.

The Collect() function doesn’t allow you to update items. It will always create new items into the given collection. That collection can be a local collection within your Power App, but also an external data source such as SharePoint.
Please note that when using SharePoint as your collection, you cannot use the ClearCollect() function.

Collect(
    TestList,
    {Title: DataCardValue1.Text}
)

This will create a single new item into into the SharePoint list. You can put the outcome of this Collect() action into a variable, just like we did with the Patch() action.

However, the Collect() action always returns the entire SharePoint table. So to get the details of the item you’ve created, you need to pull the latest item from that table using the Last() function:

Set(
    varCollectedItem,
    Last(
        Collect(
            TestList,
            {Title: DataCardValue1.Text}
        )
    )
)

Keep in mind that Power Apps has a default item limit of 500. So if your list exceeds 500 items, you may not get the right amount of items back from your collection. You can increase this limit to a maximum of 2000 items by going to File > Settings > General > Data row limit and change its value to 2000. If you still have more items in your list, you need to have some form of delegation in place in order to retrieve your item.

The result

When you’ve put your result into a variable, you can just use the <VARIABLE>.ID or <VARIABLE>.Title (or any other field that is inside your list) function to show specific item information into your Power App (as explained in the SubmitForm() section).

Below, you can see the result from the SubmitForm(), Patch() and Collect() actions in both SharePoint and Power Apps:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

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