cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ques0buffet
Helper II
Helper II

Capture SharePoint List Item Changes Like Version History Details Page

I'm working with a list that has 30+ fields and I need to capture what fields have been updated and output it to another list or table.  Using the Get changes for an item action, is it possible to output only the fields that have changed and their value?

My first thought was to loop on or filter the output of the Get changes action, then somehow get the associated field values.

16 REPLIES 16
ekarim2020
Super User
Super User

The Get Changes for an item or file action that can be used to determine which column has changed.

There is a Has Column Changed dynamic content that returns true if the field was changed or false if the field did not change.

ekarim2020_1-1630013012585.png

 

ekarim2020_0-1630012845271.png

Ellis

Right, but retrieving the field values is what I'm looking for.  


"Using the Get changes for an item action, is it possible to output only the fields that have changed and their value?"

My hope is to avoid creating a large condition or switch statement.

ekarim2020
Super User
Super User

This is possible and I can offer an approach to the problem - though other contributors with more experience than I should be able offer something better and more refined. But the following should get you started.

If we start with a simple SharePoint list called Routing - I want to know when any of the column values are changed; I want to receive an email showing me a list of the the columns that were changed along with their new values:

ekarim2020_0-1630068600470.png

If you have versioning enabled on the list, you can view an item's version history and see which columns were changed and what the new values are:

ekarim2020_3-1630071328161.png

The version history can be accessed by right clicking an item and then selecting Version history.

ekarim2020_2-1630070870416.png

If we could get access this version history via Power Automate, and access to just that subset of data as shown in the version history then our job is done and we will have what we need. But I haven't found a way to do this or it is likely that this is not currently possible with Power Automate?

For now I will have to try to do this manually, and I will post a possible approach to the issue in the next post.

See also: Get the changes made to SharePoint Items with Power Automate

Ellis

ekarim2020
Super User
Super User

Here is a demo flow along with some description of how it works. If you are able to get it to work, you can refine and merge a number of steps. Unfortunately it looks more complicated than it really is. The flow could work with almost any list.

ekarim2020_0-1630098139666.png

If we start with a simple SharePoint list called Routing - I want to know when any of the column values are changed; I want to receive an email showing me a list of the the columns that were changed along with their new values. In the example below, the Folder and Preference column have been changed for item ID 14 and I receive an email with the changes that were made:

ekarim2020_1-1630098238817.png

What I am trying to do is create an array that stores the SharePoint column names and a true/false value indicating if that column was changed or not. The array can be iterated over i.e. accessing each element of the array one by one. The array could be a JSON array but for simplicity I am going to use an array of strings:

ekarim2020_21-1630102496136.png

Here is the high-level view of the flow:

ekarim2020_2-1630098409205.png

Part 1: The trigger and get changes action:

ekarim2020_3-1630098458558.png

Initialise an array variable varListOfColumnsChanged - which will store the SharePoint column names and whether the columns were changed or not (true or false):

ekarim2020_6-1630099004810.png

We can get a list of all the columns and their changed states from the action Get changes for an item or a file (properties only). This data is given to us as a JSON object from the dynamic content ColumnHasChanged. I convert ColumnHasChanged to a string and store it in varStringData.

ekarim2020_12-1630100566440.png

Split varStringData string at the comma delimiter ',' - creating an array as you see above, in the Compose Split String at comma action.

 

outputs('Get_changes_for_an_item_or_a_file_(properties_only)')?['body/ColumnHasChanged']
outputs('ColumnHasChanged_as_JSON_Object_Data')
split(variables('varStringData'),',')

 

Part 2:  After the split action we need  to clean up the column names in the array by removing escaped strings (eg. " double-quotes got replaced with  \"  when the JSON object was converted to string) as well as removing other unwanted characters such as the '{' and '}' characters. The high level view of this section is shown below:

ekarim2020_8-1630099402261.png

ekarim2020_0-1630105727320.png

The escaped strings \" are removed along with { and } characters as you see above in the green box.

 

replace(items('Apply_to_each'),'"','')
replace(replace(outputs('Sanitise_Item_Step_1'),'{',''),'}','')

 

A little more tidy up to separate the SharePoint column name and its change state value using split:

ekarim2020_11-1630100001151.png

The split expression is:

 

split(outputs('Sanitise_Item_Step_2'),':')

 

The expression for Append to array variable varListOfColumnsChanged is:

 

first(outputs('Compose_Split_on_Colon'))
last(outputs('Compose_Split_on_Colon')

 

Note the colon between the two expressions:

ekarim2020_14-1630100930176.png

Part 3: Finally we check the SharePoint column name (key) and its change state value, and if it was changed, we store this column name in a variable - which I will use to email a list of all the changed columns. The high level view of this section is shown below:

ekarim2020_15-1630100995267.png

ekarim2020_16-1630101039830.png

 

ekarim2020_0-1630107097764.png

Example of the output that was generated:

ekarim2020_20-1630101561753.png

The flow could work with almost any list, you would just need to change the List/Library names. In the example below I connected the flow to an Issue tracker SharePoint list:

ekarim2020_22-1630104348957.png

ekarim2020_23-1630104364264.png

I get the following results, though you will need to handle the the values in complex columns:

ekarim2020_24-1630104488645.png

Hope this helps.

Ellis

 

outstanding post!   thank you for taking the time to write all of this up.

ekarim2020
Super User
Super User

I was able to finally refactor the flow to fewer steps. This makes the flow shorter and easier to understand. All credit to Identify which SharePoint item columns were updated in Power Automate by Tom Riha. 

ekarim2020_0-1630145740775.png

(1)  Add the trigger and Get changes actions:

ekarim2020_1-1630146040993.png

(2) Initialise a string variable for the email body:

ekarim2020_2-1630146203119.png

(3) Convert the object HasColumnChanged to string, and split at the comma delimiter. We filter only the items that contain string ':true'

ekarim2020_3-1630146271847.png

 

From:
split(string(outputs('Get_changes_for_an_item_or_a_file_(properties_only)')?['body/ColumnHasChanged']),',')

item()
contains
string(':true')

 

Sample output:

ekarim2020_13-1630148732770.png

(3) Use json() to do all the clean-up! From "\"Title":true" to "Title" using json(item()):

ekarim2020_6-1630146742493.png

Sample output:

ekarim2020_14-1630148771934.png

(4) Constructs the email body containing the changed columns and their values:

ekarim2020_12-1630148562603.png

 

body('Select')
item(): triggerBody()[item()]

 

ekarim2020_9-1630147345659.png

Sample output:

ekarim2020_11-1630148233653.png

Ellis

While this works great, it fails when a field is empty

ekarim2020
Super User
Super User

Hi @msabau ,

 

At what point in the flow does it fail? We may be able to adjust the expressions to handle missing values.

 

Ellis

Solved it by following the guide you linked above. The only problem is that that tutorial gets rid of the values. For the moment I don't need them, but if i'll ever need to know the values that were changed, I'll try to figure it out on my own

Basically this is the error:

 

InvalidTemplate. Unable to process template language expressions in action 'Append_to_string_variable_2' inputs at line '0' and column '0': 'The template language expression 'triggerBody()[item()]' cannot be evaluated because property 'Email' doesn't exist, available properties are 'Ottienielemento_ID'. Please see https://aka.ms/logicexpressions for usage details.'.

 

It happens when I try to use triggerbody to append the value of the field to the string.

Not really sure how to fix it at the moment.

 

EDIT: I guess this happens because my trigger is a button in a powerapp, not the same trigger as your guide. I'll try fix it now

ekarim2020
Super User
Super User

Hi @msabau .

 

Here is something to try.  If you update the expression in question (step 4) to the following:

Old:     triggerBody()[item()]

Updated: triggerBody()?[item()]

 

when the property is not found, a null value (empty) should be returned, and the flow should continue. For more info, see How to query JSON data using JSON notation with Power Automate

 


Ellis
____________________________________
If I have answered your question, please mark the post as Solved.
If you like my response, please give it a Thumbs Up.

Thank you for your reply. With the updated expression the flow doesn't stop, but it doesn't display values

ekarim2020
Super User
Super User

 

Yes, because the expression triggerBody()?[item()] is evaluating to null or empty. This means that the property triggerBody()?['Email'] does not exist in the triggerBody().

 

Check your data for the correct property name. 

Example, triggerBody()?['Author']:

{
  "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
  "Claims": "i:0#.f|membership|ellis.karim@dev365.com",
  "DisplayName": "Ellis Karim",
  "Email": "ellis.karim@dev365.com",
  "Department": null,
  "JobTitle": "Business Manager"
}

triggerBody()?['Author/Email']: 

 ellis.karim@dev365.com


Ellis
____________________________________
If I have answered your question, please mark the post as Solved.
If you like my response, please give it a Thumbs Up.

Thank you for your reply. I'm wondering if there is a way to cycle through all the properties so that I don't have to type them all manually

I finally figured it out. Thank you for your tremendous help

msabau
Advocate III
Advocate III

If anybody else has the same issue I had or just want to greatly simplify the flow, instead of Sanitising the JSON, you could just do this:

if(contains(string(triggerBody()?[item()]), 'Value'), triggerBody()?[item()]?['Value'], triggerBody()?[item()])
If an item comes from a multiple choice field, meaning that it contains the word "Value", then it only extracts said value, otherwise it just gets the regular value from regular fields. I used this in the stage just before composing the email and you basically get rid of all the sanitisation

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Users online (4,674)