Power Automate - How to get previous value of updated SharePoint List Item
Tags: powerautomate, sharepoint, sharepointonline, powerfuldevs
Published: 2023-04-13 09:28:33

Currently at time of writing, it is easy to kick-off a Power Automate flow when a SharePoint list item value changes and retrieve the new value.

However, to retrieve the previous value will require some work.

Below is the solution.

## Overall Flow

![Full Flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ze5n5uhn3uf6y78ynwn.png)

---
---

## Flow Breakdown

Flow Type: **Automated cloud flow**
<br/>


### Step 1 - Trigger when an item or a file is modified

Trigger Type: **When an item or a file is modified**

![Trigger](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pai6csjyn09rmwpto0ck.png)

---


### Step 2 - Get changes for an item or a file (properties only)

**Purpose:** To retrieve previous version number
**Action Type:** Get changes for an item or a file (properties only)

![Get changes for an item or a file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p58jmwcev1pms32mwoy4.png)

The **Since** value will be the **Trigger Window Start Token**

![Trigger Window Start Token](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/scw4c5g98dtta9jjgezt.png)

---


### Step 3 - Send an HTTP request to SharePoint

**Purpose:** To retrieve all the versions of this particular updated list item
**Action Type:** Send an HTTP request to SharePoint

![Send HTTP request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l06nc5sv9063ij10icgs.png)


- **Uri**: `_api/web/lists/getByTitle('{Your_list_name}')/items(@{triggerOutputs()?{List Item ID from Step 1})/versions`
- **Headers**: 
  - Key: `Accept`
  - Value: `application/json; odata=nometadata`

** It is important to add the `Accept` header to return JSON, else it will return as XML by default

---

### Step 4 - Do a Test Run 

**Purpose:** To get sample JSON which will be used for your next action.

After running, copy the JSON from the **Outputs** body section

![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gz3bhbbl0b3bz62fyu6m.png)

---

### Step 5 - Parse JSON response

**Purpose:** Turn the JSON response into objects which we can then use
**Action Type:** Parse JSON

- **Content:** `Body` of the **Send an HTTP request to SharePoint**  action
- **Schema:** 
  - Click on **Generate from sample**
  - Paste the JSON copied from Step 4 and click on **Done** button
![Sample JSON](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/km6b6hh8pb02xc2w81vh.png)
  - Once outside, click on **'Generate from sample'** button to fill up the **Schema** field

---

### Step 6 - Retrieve version of item 

**Purpose:** Retrieve only the specific item version from the array
**Action:** Filter array

![Retrieve item](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmgtk22ugajwewg0lh8x.png)

- **From:** `value` from **Parse JSON** action
- **Where:**
  - `VersionLabel` from **Parse JSON** action 
  - **Condition:** `is equal to`
  - **Value:** `outputs('Get_changes_for_an_item_or_a_file_(properties_only)')?['body/SinceVersionLabel']`. This value is from **Step 2 - Get changes for an item or a file (properties only)**. There is no need to manually type this. Click on the **Expression** icon and choose the following in the popup window.

![Expression Window](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3649mcy2r4qinos502pl.png)

---

### Step 7 - Retrieve value from the item version

**Purpose:** Retrieve value from old version and setting value to variable 
**Actions:** Initialize Variable & Set variable (within Apply to Each control)

Here when setting the variable, you can select the column you want, in this case it was my `Project` field.

I then set the `ProjectOldValue` variable with the `Project` value.


![Setting project value](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54x7mwvxy6ys5lf9t8js.png)



---
---

Note that this flow can be modified as required for various scenarios, one being, if we need to retrieve the original value for an item (1st version), we can just hardcode the version value `1.0` for `VersionLabel` for **Step 6**.