Sunday 2 January 2022

Power Automate: Update file metadata and preserve version history.

In this blog, we will be updating file properties/metadata whenever a new file is uploaded into the library.

In this approach, we will use the REST API and wrap it into a Power Automate flow.

Create Flow

Complete flow with all actions


Flow URL - https://flow.microsoft.com/

1. Open the flow, click on the Create option, and select "Automated Cloud Flow".


Enter Flow name.
Choose the flow trigger as "When an item is created".
Click the Create button.



2. Enter the "Site Address" SharePoint site URL and the "List Name" library title where flow needs to be associated.

Add a new action step, "Compose" and rename it "Compose-user".

In input, add an object as shown below. Created By Claims is value; here we will get the claims value of the user who is uploading the file.



3. Add a new action step again, "Compose" and rename it as "Compose-update".

In input, add an object as shown below.

  {

    "FieldName": "Internal column name",

    "FieldValue": "desired value"

  },

  {

    ............

  },

  .....

]

The input value will be an array of objects, and each object defines the configuration of the column to be updated. The FieldName is the internal column name, and the FieldValue is the desired value that you want to set in that column.

In my example, the Editor (Modified by) and Title columns are updated.

In the Editor FieldValue output of Compose-user will be set.

Note: Flow is running under the service account. If the Editor field is updated, then we can also preserve the Modified By column. Otherwise, the service account value will be updated in the Modified By column.



4. Add a new action step: Send an HTTP request to SharePoint.

Here we will compose a REST API request call.

  • Site Address: SharePoint site URL where the library is present
  • Method: POST
  • Uri: _api/web/lists/getbytitle('<List Title>')/items(<ID>)/validateUpdateListItem
  • Body: 

{
   "formValues": outputs('Compose_-_update'),
   "bNewDocumentUpdate":true
}


        validateUpdateListItem: REST API ValidateUpdateListItem() function to update listitem metadata

        formValues: The value must be the output of the second Compose action.

        bNewDocumentUpdate: true (Not to create any version on update) or false (Create a version on the update)

        For details refer: https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-visio/jj246412(v=office.15)



        Upload the file to the Documents library and test the flow.


        🚀 "Happy Coding" 🚀