for Microsoft Power Automate and Azure logic apps

Working with File Contents and Files in Power Automate

By Jay Goodison

20th July 2020

Working with File Contents and Files in Power Automate

Like almost all connectors, the Encodian connector for Microsoft Power Automate and Azure Logic Apps receives and sends files as Base64 encoded strings. This is a really important concept to understand when working with files in Power Automate and Logic Apps.

A common misconception is that an action performed by a Power Automate connector (like Encodian) is performed in situ, i.e. the connector accesses a file directly and performs the configured action. This is not the case: you need to configure your Flow to obtain the target file from its storage location, pass this file to a connector for processing, which would then return the processed file… then finally you would then do something with the file, like adding to SharePoint.

Consider this example which shows how to convert a document stored within SharePoint to PDF using the Encodian connector:

A copy of the file is retrieved from SharePoint and sent to Encodian for processing, the document is leaving the Microsoft 365 tenancy to be processed. This is not specific to Encodian, this is how connectors interoperate within Microsoft Power Automate.

Encodian’s infrastructure is hosted 100% within Microsoft Azure data center’s, your data will never leave a Microsoft data center. Where required; we can provide a ‘Dedicated Connector‘ where a copy of the Encodian connector is deployed directly into your Azure tenant, therefor data never leaves your Microsoft cloud tenancy.

Exploring Power Automate ‘File Contents’ in detail

Let’s explore the previous example in more detail to understand how the file is processed:

Hopefully this screenshot depicts the data flow succinctly and exposes some of the clever processing performed by Microsoft behind the scenes.

You’ll notice in the first completed action that the ‘File Content‘ property returned by the SharePoint ‘Get file content‘ action contains more than just a Base64 string:

The ‘File Contents‘ schema explained:

{
    "$content-type": "<MIME Type>",
    "$content": "<Base 64 String>"
}

$content-type is a MIME Type (or media type), which helps to describe the document format (more details here); $content is the Base 64 string, the actual file encoded (more details here).

You may also have noticed that the data sent to the Encodian connector in the example is just the $content value and not the full ‘File Content’ property value:

Microsoft Power Automate is intelligently processing data to ensure that data is sent in the correct format. This is a super important concept to consider especially when you are working with documents using different actions.

This processing is also performed on the value returned by the Encodian action, whereby Microsoft Power Automate is automatically transposing the Base64 string into a complete ‘File Contents‘ property.

But why is this important?

For most simple scenarios this isn’t something you need to consider, but when you are processing files received in different ways (HTTP Post, PowerApps) or where you are collating an array of documents for merging things can go awry… very awry!

The key consideration is that Power Automate is performing pre/post-processing on Base64 strings to ease interoperability, and upon occasion, you need to add extra steps to your flow to make sure that files are not corrupted en-route. For example, consider this example of building an array of documents:

We’ve already seen that Microsoft dynamically extracts just the $content value… so the ‘Append to array variable‘ should append this:

"file": "<Base 64 String>"

But it doesn’t – see below:

Power Automate is reacting to the destination, and in this instance has decided that an array variable is expecting binary data and therefore has tried to append a binary value. Thankfully, we can use expressions to override the default behaviour and this example shows how the Base64() expression is used to inform Power Automate to provide data in the desired format:

And the resulting output is now correct:

Finally…

Hopefully this post has provided some additional insight into how Microsoft Power Automate processes files. Should you experience issues you should always check the run history and validate that data is passed correctly… it’s usually very clear when documents (File contents) are being passed in an incorrect format.

We hope you’ve found these quick tips useful, and as ever, please share any feedback or comments – all are welcome!

4 Comments

  1. lochy says:

    Hey,

    I was having trouble grabbing file content from Onedrive/teams chat files and was wondering if you can see anything I might be missing.
    I define an array variable and with a onedrive ‘get file content’ search add the data:
    “file”:”@{body(‘Get_file_content’)}”
    My array ends up looking like:
    {“name”:”Attachments”,”value”:”\”file\”:\”\”JVBERi0xLjYNJeLjz9MNCjU3IDAgb2JqDTw8L0xpbmVhcml6ZWQgMS9MID……
    But this seems to break the flow and it never finishes running, any ideas?

  2. Robin James says:

    Hi Jay,

    How would you handle the use case where the word doc is converted to pdf, but how would we maintain meta data of the file eg columns etc after conversion(similar to copy file action , where document is copied along with metadata, but while converting we create new file file and hence we loose the )

    1. Jay Goodison says:

      Hi,
      The Encodian connector is not directly connected to your Office 365 Tenant.
      You would use the SharePoint ‘Create File’ action to add the PDF rendition to SharePoint, and use other SharePoint actions (Get file properties and Update file properties) to copy the metadata values from the source document to the PDF rendition: reference, https://docs.microsoft.com/en-us/connectors/sharepointonline/#actions
      HTH
      Jay

Leave a comment

Your email address will not be published. Required fields are marked *