Tuesday 30 October 2018

Get File content action in Microsoft Flow using File Identifier and File Path

I was working on a HTTP Triggered Flow that copies a file from one document library to another. I wanted the source and target path of the file copy operation to be dynamic and could also be different site collections, so these paths were sent through the HTTP request body. However, the Flow threw errors that it could not find the Source file. This was because I was using the source file path as-is in the "Get File Content" action.


The Flow that I was building would look something like this




























The request body is parsed using the Parse JSON action and the values can be used in the "Get File Content" and the "Create file" actions.

Ideally when the source and target are pre-determined, the flow looks like below.



























Now in the Get File content action, the File Identifier is not the same as File path although it looks like it in the UI. We will see this in the upcoming steps.

The request body that we have sent through the HTTP request looks like below. It has all the data that we have used in the two file actions above.
























So now we use these values in the "Get File content" and "Create file" actions.






























Simple ! but when I execute the Flow, I keep running into an error in the Get File Content action.

 "message""The response is not in a JSON format.",
 "innerError""The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."



To check the "identifier" value, I used the "Get Item" action

































So with the identifier value, the Flow worked without any error ! The output of the Compose action shows the identifier value

























If you notice the "Get File Content" action, we need to input the "File Identifier" and that is why the path that we used from request body did not work.

Then I discovered there is already an action - "Get File Content using path" to directly use the path that we are getting in the HTTP request body.




















OR

Since the identifier is encoded, we can encode the "SourceFileUrl" that we have from the request body and use it in the Get file content action. 

However if you notice, the identifier returned by the "Get Item" action, the spaces are encoded as "%2b" ie "+" and "encodeUriComponent" expression will simply encode the spaces as %20

Using the Get File content using path action is clearly simpler option
























encodeUriComponent(body('Parse_JSON')['SourceFileUrl'])


No comments:

Post a Comment