Compare Microsoft Word Documents with Power Automate

April 26th 2021
Back to all blogs

Our team have been working hard over the past few months to build and release our latest action for the Encodian connector, Compare Word Documents.

The ‘Compare Word Documents‘ action provides document comparison capabilities by generating a comparison document containing the differences between the two source documents provided (Tracked changes in Microsoft Word).

This post shows how to compare two versions of a Microsoft word document held with a SharePoint library.

Compare SharePoint Document Versions Automatically

NOTE:

The following steps can be performed on DOC, DOCX, DOT, DOTX, DOCM, DOTM, RTF and PDF Files. However, PDF comparison is limited to Microsoft Word native support for comparing such files.

1. Create a new ‘Automated cloud flow‘ flow in Power Automate

1.a. Flow name: Provide a name for your flow

1.b. Trigger: Select the ‘When a file is created or modified (properties only)‘ SharePoint trigger action

1.c. Click ‘Create

2. Configure the ‘When a file is created or modified (properties only)‘ SharePoint trigger action as required:

3. Add a condition action and configure it to check whether the current document is a word document by evaluating the ‘Filename with Extension‘ property.

3.a. Input value: Select the ‘Filename with Extension‘ property provided by the ‘When a file is created or modified (properties only)‘ SharePoint trigger action

3.b. Operator: Select the ‘ends with‘ operator

3.c Value: Enter ‘docx

4. Within the ‘If No‘ branch, add a ‘Terminate‘ action

4.a. Status: Set to ‘Succeeded

Next, we will add logic to the flow to check the previous version of a Microsoft word document with the current version of the same document.

The SharePoint library has been left with the default version configuration (major versions only).

5. Within the ‘If Yes‘ branch, add another ‘Condition‘ action

5.a. Input value: Select the ‘Version number‘ property provided by the ‘When a file is created or modified (properties only)‘ SharePoint trigger action

5.b. Operator: Select the ‘is not equal to operator

5.c Value: Enter ‘“1.0”

6. Within the ‘If No‘ branch, add a ‘Terminate‘ action

6.a. Status: Set to ‘Succeeded

NOTE:

You may have noticed that the value passed to the previous condition has changed to 1.0 instead of “1.0”. This is just the Power Automate UI changing how the value is rendered. Still, you must not enter 1.0 as opposed to “1.0” if you do this, Power Automate will convert the value of 1.0 to 1, and therefore, the condition will not evaluate correctly.

7. Within the ‘If Yes‘ branch, add a SharePoint ‘Get File Content‘ action

7a. Site Address: Set as per the trigger actions value

7.b: File Identifier: Select the ‘Identifier‘ property provided by the ‘When a file is created or modified (properties only)‘ SharePoint trigger action

Unfortunately, there isn’t a native Power Automate action to retrieve the previous version of a document from SharePoint. Therefore we’ll use the SharePoint ‘Send an HTTP request to SharePoint‘ action.

8. Add a SharePoint ‘Send an HTTP request to SharePoint‘ action

8. a Site Address: Set as per the trigger actions value

8.b. Method: Select ‘GET.

8. c Uri: Follow these instructions:

The URI is well… complicated and very sensitive! The schema of the URI is as follows:

/_api/web/getfilebyserverrelativeurl('<ServerRealtiveFilePath>')/versions('<versionNumber>')/$value

Example below:

/_api/web/getfilebyserverrelativeurl('/sites/demo/demo.docx')/versions('512')/$value

Simple! but a little fiddly to configure Power Automate to generate the correct values. The <versionNumber> value is not the 512th version. It is version 1.0… and version 2.0 is 1024, version 3 .0 is 1536 so on. To obtain the correct value, we need to obtain the current version number value, subtract 1 (to get the previous major version) and then multiply the result by 512 using a Power Automate expression, see below:

mul(sub(int(substring(triggerOutputs()?['body/{VersionNumber}'],0,indexOf(triggerOutputs()?['body/{VersionNumber}'],'.'))),1),512)

Ensuring your flow actions are exactly as per this flow, copy and paste the following into the URI field:

/_api/web/getfilebyserverrelativeurl('/sites/demo/@{triggerOutputs()?['body/{FullPath}']}')/versions('@{mul(sub(int(substring(triggerOutputs()?['body/{VersionNumber}'],0,indexOf(triggerOutputs()?['body/{VersionNumber}'],'.'))),1),512)}')/$value

You will need to update the ‘/sites/demo‘ value to the URL of the site collection/web you are working with.

9. Add the Encodian ‘Compare Word Documents‘ action

9.a. File Content – One: Select the ‘File Content‘ property provided by the ‘Get File Content’ SharePoint trigger action

9.b. File Content – Two: Select the ‘body‘ property provided by the SharePoint ‘Send an HTTP request to SharePoint‘ action

The Encodian action will return a Microsoft Word document detailing the differences between the two documents.

For this example, I’ll simply email the resulting document for review.

10. Add the ‘Send an email (V2)‘ Office 365 Outlook action

10.a. Configure the ‘To’, ‘Subject‘, ‘Body‘ and other fields as required

10.b. Attachments Name – 1: Select the ‘Filename’ property provided by the Encodian ‘Compare Word Documents‘ action

10.c. Attachments Content – 1: Select the ‘File Content‘ property provided by the Encodian ‘Compare Word Documents‘ action

Your final flow should look very similar to:

Now execute your flow by uploading or creating a new document version:

And the document:

Final thoughts…

Hopefully, this post provides an excellent example of how you can utilise the Encodian ‘Compare Word Documents‘ action within Power Automate to compare Microsoft Word documents.

We hope you’ve found this guide useful. As ever, please share any feedback or comments. All are welcome!

Author
Jay Goodison

Managing Director

Back to all blogs

You might also be interested in...