web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Updating SharePoint li...
Power Apps
Answered

Updating SharePoint list from local collection

(0) ShareShare
ReportReport
Posted on by 234

I am building an app to allow field officers to collect and UPDATE data about remote sites where online connectivity is patchy at best. There are some very simple examples to allow new data to be added offline and update later when online but it gets more complicated when a lot of the time you are checking and updating existing data. About 300 sites are visited repeatedly. The field officer needs to check whether data on a SharePoint list is up to date and if neccessary correct it or add a record for a new site.

 

Each site is described by about 25+ columns in a SharePoint list called TableSites. Column colSiteCode is a unique identifier Several field officers may be working on different sites at the same time but two field officers would never be independently visiting the same site so wouldn't be updating the same the record at the same time so there are no worries about conflicts.

 

I have seen versions of what seems to be the same solution that allow you to copy data down while online, work on the offline copy and use Patch() to copy the data back when you are online.  The examples I have seen have only two or three columns. The problem with Patch(), at least in the examples I have seen, is I would need to specfy the names and values for all 25+ columns. This feels clumsy, makes it too easy to make a mistake and too difficult to change the columns in the future as any column I add or rename in SharePoint will also have to be renamed in the app. Is there a different function or a way to use Patch() that adds all the values of this row of the collection to the list or update all the values of the list to match the values in the collection without specifying all the column names? To make things easy LocalSites is made from TableSites and OfflineSites is made from LocalSites everytime there is a connection so the column names, order, type, etc should all be the same.

 

The solutions I have seen also assume you will go online and upload the data before you close down the app - that isn't always possible. I think it is safer to assume when the app opens it needs to check for any updates to the server before overwritting the local dataset from the server. That is easy enough for me to do.

 

My App started life as a standard three screen app built automatically based on the SharePoint list but with these changes.

 

When app opens it copies TableSites to collection LocalSites and saves it to "OfflineSites". Items for the browse screen gallery were changed to selected data from LocalSites instead of TableSites. Various other corrections were then neccessary to point at LocalSites instead of TableSites but basically it all works.

 

When a record is changed colStatus is changed from, "Online", (i.e. the record is already correct online) to, "Updated", if any column is changed or, "Added" for a new record When a record is changed or added or the refresh button is clicked or the app is opened (and possibly on a timer too) I open a screen called UpdateScreen

 

UpdateScreen.OnVisible checks if the app is connected using If(And(Connection.Connected,CountIf(LocalSites,Not(colStatus = "Online")) > 0), //then do some stuff (Depending how quick this process runs I might add a pretty countdown timer on UpdateScreen. If there is no connection there could also be a reminder to go online soon and upload recent data)

 

HELP PLEASE!

The stuff I want to do but am stuck are:

For rows in LocalSites where colStatus = "Add", add a copy of the whole row to TableSites

For rows in LocalSites where colStatus = "Update", find the correct row in TableSites and update all the columns to match the values in LocalSites Update colStatus to, "Online", as each row is updated so that row isn't used to trigger an update again.

HELP PLEASE!

 

I will then Clear collect all the data back from TableSites to LocalSites and save a new copy to, "OfflineSites", to make sure the Field Officer has the latest version of data for the site they will visit next

 

Thank you very much for your help. Guy

Categories:
  • BrianS Profile Picture
    2,407 Super User 2024 Season 1 on at

    Not sure I can answer all your questions, but as far as the Patch is concerned, you can Patch an entire collection to a SP list. As long as the lists have the same columns, you do not need to specify the columns to match. In your case it may work to check the signal when starting the app and load new data if it is online. If your users have signal at home or on the way to the first site, they could run the app then. At that point you could use SaveData to keep a local copy. That could be loaded up if the app is offline. Then you could push data into various collections depending on whether it is new data or updates. That data could be saved using SaveData as well. Then when the app is on-line you can Patch the new data, and as long as you have the record numbers, you can update the data that just needs to be changed.

  • Guy Boswell Profile Picture
    234 on at

    Hi Brian,

    Thank you very much. I think I only really have one question and you have answered that, at least in part.

    What would be the syntax to patch a whole record?  I want something along the lines:

    If connected and colStatus = Update then

    Patch(List from collection where ID matches)

    But I can't work out what should be in the brackets?  And I assume I need two different patch statements, one for update and one for add?

    Thanks.  Guy

  • BrianS Profile Picture
    2,407 Super User 2024 Season 1 on at

    It has been a little while, but my recollection is Patch(SP_List,collection) is all you need. However, you need to strip out the auto-generated columns when you build the collection. Use the DropColumns command when you read in your collection from the SP list. Strip out the Created, CreatedBy, Modified, etc. - all the stuff you didn't create that is listed at the bottom of the "List Settings" page.

    Then you can use the above Patch command for the records that are new. 

    The ones that just need to be updated will be a little trickier - that is why I suggested using 2 different collections. The updates you will need to Patch back to the SP list using their record numbers. You could do a test and see, it could be that if Patch sees the record number already exists it will edit that record. It has been too long since I did this to remember.

  • Guy Boswell Profile Picture
    234 on at

    Wow!  I set up a list with 5 columns including the default Title.

    In SharePoint I can see 9 columns (Modified, Created, Created by and Modified by have been added).

    I added Collect(LocalSites,TableTest) to App.Onstart

    When I look at the collection it has 25 columns!!!!

    So I will need to drop 20 columns before I can patch data back to the list

    The problem is most of the columns only the start of the name is displayed.  So I don't actually know what column names I need to drop?

  • BrianS Profile Picture
    2,407 Super User 2024 Season 1 on at

    You should be able to use ShowColumns with just the main columns - not the "created" columns

  • Guy Boswell Profile Picture
    234 on at

    That OK but cumbersome with test list with only five columns.  Real list has 25 real columns (as well as it seems 20 added columns).  If I need to type in all 25 column names I might as well add all 25 to the patch statement.  But that is UGLY!

  • v-xida-msft Profile Picture
    Microsoft Employee on at

    Hi @GuyBoswell ,

    Do you want to patch your local LocalSites collection data back to your SP List?

    Do you not want to specify all column names within the Patch function?

     

    Regarding the needs that you mentioned, I think the combination of Patch function and ForAll function could achieve your needs. On your side, please try the following formula:

    If(
     Connection.Connected && CountIf(LocalSites,Not(colStatus = "Online")) > 0,
     ForAll( // write the Patch formula as below
     LocalSites As LoopRecord,
     If(
     LoopRecord.colStatus = "Update",
     Patch(
     TableSites,
     LookUp(TableSites, colSiteCode = LoopRecord.colSiteCode),
     LoopRecord,
     {
     colStatus: "Online"
     }
     ),
     LoopRecord.colStatus = "Add",
     Patch(
     TableSites,
     Defaults(TableSites),
     LoopRecord
     )
     )
     );ClearCollect(LocalSites, TableSites);...
    )

     

    More details about the As operator, please check and see if the following blog could help in your scenario:

    https://powerapps.microsoft.com/en-us/blog/formulas-thisrecord-as-and-sequence/

     

    Regards,

  • Verified answer
    Guy Boswell Profile Picture
    234 on at

    Thank you all for your helpful suggestions but Patch() seems to work OK without any complexity of hiding and showing columns and looping and all the rest of it.  I am still experimenting

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 381 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 340

#3
WarrenBelz Profile Picture

WarrenBelz 187 Most Valuable Professional

Last 30 days Overall leaderboard