07-20-2023 17:52 PM
SharePoint Batch Update, Create, & Upsert Template
(Also an option for full dataset synchronizations with the "Full Sync" template below)
Compared to the using the basic Apply to Each, Get items, & Update item approach on large amounts of data, this method requires a small fraction of the API calls towards the daily action limit and drastically reduces flow run-times.
It's currently set to take data from any Excel sheet and update records in SharePoint with corresponding data. It works with any potential Power Automate data-source, including HTTP API GET requests, but Excel provides a simple example.
Part of it works like a Vlookup function where it identifies if a row of updated data in Excel or another datasource matches another SharePoint key column and gets the SharePoint record ID for that match. Then it uses the batch update method to update those SharePoint records and it uses the batch create method to create new items for any records without a match.
David Wyatt's Flow Optimization Post For Loading SharePoint Records: https://www.linkedin.com/pulse/top-5-ways-optimize-your-flows-david-wyatt/?trackingId=X9bMmnTZ2QBuu4...
Microsoft Batch API Documentation: https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/make-batch-requests-with-the-rest-apis
TachyTelic.Net Blog & Videos
SharePoint Batch Create Flow
Blog: https://www.tachytelic.net/2021/06/power-automate-flow-batch-create-sharepoint-list-items/
Video: https://youtu.be/2dV7fI4GUYU
SharePoint Batch Delete Flow
Blog: https://www.tachytelic.net/2021/06/power-automate-delete-sharepoint-items/
Video: https://www.youtube.com/watch?v=2ImkuGpEeoo
Version 2.7 - Upsert
-Includes a batch create segment to create an upsert capability. If anyone wants to only update records, then they can remove the Batch Create section. If anyone wants to only create records, then they can go to the GenerateSPData action, remove the expression for the ID field and insert the null value expression.
-Further simplifies the set-up, removing the need to add any additional SharePoint Get items actions & removing the need for parallel branches.
-Can now work on lists with a few million items without adding more actions or branches. It also implements a faster load method using the SharePoint HTTP action as described in point 5 of this article.
-The batch loops have been changed from Do until loops to chunking into Apply to each loops so the batch actions can now run concurrently for additional speed. If you have many batches of data you want to process faster, you can try increasing the concurrency settings on the Apply to each loops containing the SendBatch actions.
-The "setting" inputs action was moved to the top of the flow to help accommodate the new streamlined set-up.
-A SP HTTP call now automatically fixes some issues with referencing the correct list name.
-Faster list load time.
-If you need to batch create &/or update hyperlink columns, check this post
-Adds another HTTP call to get the site users into an object indexed/reference-able by email addresses & gives an example of how to use that to batch update a person column. Anytime the updated source dataset has a blank or an email value not found in the top 5000 site users, it will replace any person in that item with a null value.
Updated set-up screenshots & instructions in this post: https://powerusers.microsoft.com/t5/Power-Automate-Cookbook/Batch-Update-SharePoint-List/m-p/2225500...
Full Sync V1.1 (Combined Upsert & Batch Deletion Sync)
The SharePoint Batch Full Sync template pulls in all the SP List & Source data to perform a batch upsert. But then it also adds on a Batch Deletion Sync to find & delete all the records in the SP List that are not in the Source dataset.
Now, this is initially set up to pull in all the datasource records for all batch actions in the same Do until Get source data loop. And that piece will be limited by the 100MB maximum message / variable size limits for Power Automate, so this Full Sync version will initially only work with datasources with a 100MB or less total size. But this is really only because I'm trying to keep the flow simpler for the majority of users who likely will not have datasources of many 100s of thousands of records.
If you want to further push out against this 100MB limitation, then you will need to separate out the source get data for the batch upsert section from another source get data for the batch deletion sync section. So for the batch upsert section you can use a set-up like in the main batch upsert template where it loads records with all columns 100,000 at a time (or 100 or 5000 or whatever your source dataset per load limitations are) and runs the batch upsert on each source load before running the Do until loop again to get the next source load (which avoids holding anywhere near 100MB in memory at once because it is performing things one load at a time). Then the batch deletion sync section can use a different source get data set-up similar to the "Do until Get destination list IDs + keys" section of the templates where each loop can pull a load from the source dataset & then use a Select action to select only a few of the columns to pass on to the variable holding everything in memory. Since deletions only require the primary key values, you can set the Select to only get the primary key column from each source data load & pass that onto the "Source data outputs" / variable. A full listing of all the primary key values in your source dataset will be much smaller than all columns for the entire table, so that 100MB limit should then hold a few million records worth of the required primary key data to run the batch deletion sync process.
Self Update (See the 1st comment below the main post for the zip download)
The SharePoint Self Batch Update assumes you just want to perform simple updates using only the existing data in the list and removes all the actions related to comparing two datasets to find updates. This may be much easier to use if you just want to quickly do something simple like get all the items created in the past month and mark them all with a Complete status.
But you will be limited to using just the data already in the list and any values you can manually input into the flow.
Version 1.5 - Update
This version makes it easier to handle cases where the list name may have changed since its creation and moves a few of the primary key column matching inputs to the 'settings' compose action so users don't have to look through & edit the more complicated expressions to set up the flow.
The flow can easily expand to any size of SharePoint list by adding more Get items actions and batch update parallel branches. If speed is a concern for anyone, there are ways to make the Get items actions all load in parallel too (up to 50 branches). It's really only limited by how much effort people want to put into their flows & lists.
Google Drive Link to Flow Zip Files: https://drive.google.com/file/d/10p7EB730xsEj-azVYuTIuu8dS0w-AflR/view?usp=sharing
Google Drive Link to Text File to a Scope Action Containing The Flow: https://drive.google.com/file/d/1BVGoeM5mykYlMobAyFkhuLRh3r7jMSLz/view?usp=sharing
Version 1 - Update
Version 1 Explanation Video: https://youtu.be/l0NuYtXdcrQ
Download The Template Batch Update Flow
Google Drive Link to Flow Zip Files: https://drive.google.com/file/d/10gFkycdx6zpRfrI-s_jCDwIK6dpyyDqk/view?usp=sharing
Google Drive Link to Text File to a Scope Action Containing The Flow: https://drive.google.com/file/d/1e6-INUykIT22ppVh5m4kxz8us_7qXy7q/view?usp=sharing
Formulas For Random Number Columns
SharePoint Rand1To50 Column Calculated Default Value Formula:
=INT(INT(RIGHT(NOW(),2))/2)
Excel Random Column Formula:
=ROUNDDOWN(((Rand()*100)+1)/2, 0)
If you have trouble importing any of the flows using the standard legacy import, you can also try importing a Power Apps Solutions package here: Re: Batch Update, Create, and Upsert SharePoint Li... - Page 25 - Power Platform Community (microsof...
Thanks for any feedback,
Please subscribe to my YouTube channel (https://youtube.com/@tylerkolota?si=uEGKko1U8D29CJ86).
And reach out on LinkedIn (https://www.linkedin.com/in/kolota/) if you want to hire me to consult or build more custom Microsoft solutions for you.
watch?v=QCkjQy6sHZg
In the batch update section of the flow run check the Append to string variable action in the loop. What error codes are you seeing there?
That termination means the SendBatch encountered an HTTP error on at least one line it tried to update in the batch.
This is the error message I am getting:
--batchresponse_d24bb4d4-6a9a-46db-938e-48c55aa40290
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 204 No Content
CONTENT-TYPE: application/json;odata=verbose;charset=utf-8
ETAG: "9d9396cb-3ac2-4ae5-8f7b-cfbaf46e8feb,17"
--batchresponse_d24bb4d4-6a9a-46db-938e-48c55aa40290
Content-Type: application/http
Content-Transfer-Encoding: binary
A 204 will not stop the flow & usually still succeeds. We're looking for a 400 or 500 error like...
400 Bad Request
401 Unauthorized
403 Forbidden
404 Not Found
408 Request Timeout
409 Conflict
502 Bad Gateway
You can control + f search the outputs for these numbers.
Started again with this file , and so Far looks like its working as expected - ill monitor and let you know if anything changes - thankyou so much for your work and support 🙂
Here goes. I'm new to power automate and appreciate your help.
HTTP/1.1 400 Bad Request
CONTENT-TYPE: application/json;odata=verbose;charset=utf-8
{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"Cannot convert a primitive value to the expected type 'Edm.DateTime'. See the inner exception for more details."}}}
--batchresponse_d24bb4d4-6a9a-46db-938e-48c55aa40290
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 409 Conflict
CONTENT-TYPE: application/json;odata=verbose;charset=utf-8
{"error":{"code":"-2130246326, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"Save Conflict\n\nYour changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes."}}}
--batchresponse_d24bb4d4-6a9a-46db-938e-48c55aa40290
Content-Type: application/http
Content-Transfer-Encoding: binary
The 409 error seems like there is another user or system trying to update the given record at the same time. This shouldn’t be a persistent issue unless you have the program running during a time when another user or program will be trying to update the same records.
The 400 error seems to have an issue with one of your inputs for a date/time column. Make sure the inputs for that column in the GenerateSPData map input are date/time. You may need to use the formatdatetime expression: https://youtu.be/M_lx_dzwV-s
Another common issue with this error for date/time is if you try to pass a blank string ‘’ value then it will give this error. You will want to add extra logic for that scenario that either sets a specific date value for empty values or that sets it to null. Something like…
If(empty(InsertHere), null, InsertHere)
The 400 error is gone but the list is still not updating. The only remaining errors are, one 409 error and 798, 204 errors.
@NickM10
A 204 is usually benign.
I'm not sure about the 798 error as that is more commonly listed as an Azure VPN error... "A certificate could not be found that can be used with this Extensible Authentication Protocol. (Error 798)"
A 409 error is again another user or system trying to update the given record at the same time.
You can try turning off the concurrency setting that enables several batch update loops to be processed at once and see if that does anything to help with this one...
May I use this batch process on the item level role assignment?
I have tried
--changeset_8d85a245-36c1-4030-a6dd-96ee69bb038e
Content-Type: application/http
Content-Transfer-Encoding: binary
POST https://mygroup.sharepoint.com/sites/myapp/_api/web/lists/getbytitle('MyList')/items(27)/roleassignments/addroleassignment(principalid=11,roledefid=1073741930) HTTP/1.1
Content-Type: application/json;odata=verbose
but got 400 error, which says only GET is the acceptabale HTTP operation.
Any advice?
Thanks.
@youngf I'm not 100% certain, but this page mentioned in the documentation states...
"The body of a batch request MUST be made up of an ordered series of query operations and/or ChangeSets. A query operation in the context of a batch request is either a query or Function invocation request as described in [OData:Core]. A ChangeSet is an atomic unit of work consisting of an unordered group of one or more of the insert/update/delete operations, Action invocations or Service Operation invocations described in [OData:Core]. ChangeSets MUST NOT contain query operations and MUST NOT be nested (i.e. a ChangeSet cannot contain a ChangeSet)."
Where I believe the "query operations" mentioned are all GET requests and the "ChangeSets" are limited to Create, Update, & Delete operations.
Adding in the fact it is not working for you & saying only a GET request is allowed for the operation you are attempting, while the template for this post uses POST requests to Update & Create items. All suggests to me that the operations like role assignment changes are not currently supported in the batch API.