Archive

Posts Tagged ‘File’

Canvas App | Power automate | Read and preview Dataverse ‘File’ column content

February 21, 2023 Leave a comment

In this article, I am going demonstrate how to read and preview the text content of Dataverse ‘File’ column by using Canvas App and Power automate flow.

Scenario:

To explain the read and previewing the Dataverse file column, I’ve taken following scenario.

Note : Approach in this article only works for plain text files but not for Doc,Docx,Pdf file types.

  • Create a table ‘Preview Files’ in Dataverse
  • Add a ‘File’ column by name ‘Preview Attachment‘ to upload text files.
  • Build a Canvas App, to show the ‘Preview File’ records in a gallery and on selection of a record, read the content of the ‘Preview Attachment’ column’s file and display.

In this article, I will focus more on Power Automate side of things on how the flow can be used to retrieve the file content and pass it back to Canvas App. Lets get started.

Steps to create Canvas App:

I will not be explaining steps to create Canvas app from scratch. For our scenario, at the high-level.

  • Create a new Canvas App and add a Vertical Gallery control by name galPreviewFiles.
  • Add ‘Preview Files’ Dataverse table as Datasource of the galPreviewFiles.
  • Add a Text Label control to show the preview of File content.
  • Canvas App looks as below.

Steps to build Power automate flow to fetch the Dataverse file content:

As we will be triggering flow from Canvas app by passing parameters back and forth, create a new flow from Canvas app studio by

  • From Canvas App studio, create a new flow from blank as shown below.
  • In the flow editor, add Initialize a variable control and name it as ‘varPreviewFileId’.
    • In the ‘Name’ parameter, provide name as previewField and in the ‘Value’ parameter, select ‘Ask in PowerApps’.
      • From Canvas App we will pass the selected Preview Files table’s record id.
      • We need ‘previewField ‘ variable to capture the passed id from the Canvas app.
  • Next add Download a file or an image control to download the content of ‘Preview Attachment‘ file column of the ‘Preview Files‘ table.
    • Set ‘Row ID’ as previewField.
    • Set ‘Column name’ as ‘Preview Attachment‘, which is the file column of the ‘Preview Files‘ Dataverse table.
  • Download a file or an image control provides the Response as below.
    • Response contains ‘$content-type’ and ‘$content’ parameters.
    • ‘$content’ contains the Base64 format of ‘Preview Attachment‘ file column content.
  • So in the next step, we need to convert the ‘$content’ value from Base64 to plain text.
  • Add Compose action and in the ‘inputs’ parameter write following formula using base64ToString expression.
    • base64ToString(outputs(‘DownloadPreviewFileAttachment’)?[‘body’]?[‘$content’])
    • Here DownloadPreviewFileAttachment is the name of Download a file or an image control added in previous step.
  • That’s it, we have the logic to read the ‘Preview Attachment‘ file column content and convert to string.
  • As a last step, we will pass the ‘Preview Attachment‘ file column content back to Canvas app.
  • Add Respond to a PowerApp or flow control to send the response back to Canvas app.
    • Add an output parameter by name ‘FileContent’ and in the Value select the Outputs of ‘fileContent’ compose action added in previous step.
  • Provide the name of the flow.
    • I have named my flow as FetchDataverseFileColumn.
  • Save the flow.

Trigger flow from Canvas App:

Now that we have Canvas App and Cloud flow, we simply need to trigger the flow from Canvas App.

  • Use following formula to fetch the file content of selected galPreviewFiles gallery control item.
    • Note that, I have a button ‘btnPreview’ in my galPreviewFiles gallery control. So I am triggering formula on ‘OnSelect’ of ‘btnPreview’ button.
UpdateContext({fileContent: FetchDataverseFileColumn.Run(galPreviewFiles.Selected.'Preview File').filecontent});
  • In the formula,
    • I am calling FetchDataverseFileColumn.Run() to call the FetchDataverseFileColumn flow and also passing the galPreviewFiles selected record’s Id.
    • Capturing the Output of the FetchDataverseFileColumn flow in to fileContent variable.
  • Set the fileContent variable to the Text Label control.

That’s all. Save and Publish your Canvas app.

Disclaimer :
  • As mentioned in the Notes, approach in this article only works for plain text files but not for Doc,Docx,Pdf file types.
  • Purpose of this article is to make aware of calling flows from Canvas app and usage of Download a file or an image control.

🙂

Advertisement