Merge Attachments from Power Apps Attachment Control and Send results via email

In our previous post, we used The Power Apps Attachment Control and a Gallery Control to collect all the attachments from a SharePoint List and then store the collected attachments in JSON format in a variable.

Before we begin with Part 2, please make sure the following prerequisites are in place:

  1. Muhimbi PDF Converter Services Online full, free or trial subscription (Sign up).
  2. Appropriate privileges to create Flows.
  3. Working knowledge of SharePoint Online, REST API and Power Automate(Microsoft Flow).

Now that we have our prerequisites in place, let’s pickup where we left-off in our previous post, Associating a Power Automate Flow to a button.


Step 1:

  1. Click on ‘Create a new flow’ in your Power App.
  2. In the new tab, The Power Automate templates section should open.
  3. Select the ‘PowerApps button’ template(screenshot below).

Step 2: On the Flow designer you should see the PowerApps Trigger.


Step 3:

  1. Add the ‘Compose’ action and rename it to ‘FileData from Power Apps’.
  2. In the Compose ‘Inputs’ section, pass the ‘Ask in PowerApps’ function to the output of the PowerApps trigger.
  3. Give your Flow a meaningful name (the name of my Flow is PowerBootCamp(Merge)), then save the Flow.

Step 4

  1. Navigate back to the PowerApps designer.
  2. Select the Button Control  ‘Trigger Merge Flow’, select the ‘Action’ tab and click on ‘Power Automate’.
  3. Select the Flow that you just created.

Step 5:

For the ‘Trigger Merge Flow’ button ‘OnSelect’ Function, pass the following  formula.

'PowerBootCamp(Merge)'.Run(FileData);
Reset(Attachment_Control);
Exit(true)

Details of the Formula associated to Button “Trigger Merge Flow” can be found below:

  • ‘PowerBootCamp(Merge)’.Run(FileData).
    • ‘PowerBootCamp(Merge)’is the name of our Flow.
    • ‘FileData’ will hold the File Content as a JSON variable.
  • Reset(Attachment_Control)
    • Resets a control and will clear all the Attachments from the Attachment control.
  • Exit(true)
    • Close the Power App.

Run your Power App and click on the ‘Collect Attachment Data’ button and then on ‘Trigger Merge Flow’.


Step 6:

Navigate back to your Flow Runs screen and open the succeeded flow.

  1. On the Flow designer, expand the Compose ‘FileData from Power Apps’ action and copy the output to the clipboard.

Step 7:

Add the Parse JSON action.

  • Content: Add the ‘Outputs’ the output of the ‘Compose’ action.
  • Schema: Click on ‘Generate from Sample’ and paste the contents that we copied from Step 6.

Your final ‘Parse JSON’ action should look like the screenshot below:


Step 8:

Initialize two variables with reference to the screenshot below.

  • The ‘FileContent’ variable will be of type ‘String’.
  • The ‘Files’ variable will be of type ‘Array’.

Step 9:

Add the ‘Set variable’ action.

  • Name: ‘FileContent’ is, the variable we created in the above step.
  • Value: ‘File’ is the output of the ‘Parse JSON’ action.

Note: The ‘Apply to each loop’ will automatically be added.


Step 10:

In the ‘Apply to each’ loop add a ‘Compose’ action and rename it to ‘Split Function’.

  • Inputs: Add the Expression “split(variables(‘FileContent’),’,’)[1]”

Step 11:

In the ‘Apply to each’ loop, add a ‘Compose’ action and rename it to ‘Replace Function’.

  • Inputs: Add the Expression “replace(outputs(‘Split_Function’),’”‘,”)”

Step 12:

Add the ‘Append to array Variable’ action.

  • Name: Files
  • Value:
{
  "source_file_name": @{items('Apply_to_each')['FileName']},
  "source_file_content": @{outputs('Replace_Function')}
}
  • “source_file_name”: “FileName” is the output of the Parse JSON action.
  •  “source_file_content”: “Outputs” is the output of the Replace action.

Step 13:

Add the HTTP action.

  • Body:
{
  "use_async_pattern": fals
  "document_start_page": "Starts on the default page",
  "source_files": @{variables('Files')},
  "fail_on_error": true
}

Note: ‘source_files’ : ‘Files’ is the output of the ‘Append array to variable’ action.

Now, you might have a question-  Why are we using HTTP action and not using the ‘Muhimbi Merge’ action directly in the Power Automate(Microsoft Flow)?

The answer is that on deployment to end users, each user will need their own account in the Muhimbi system to authenticate with The Muhimbi Flow Connector, or they all need to enter a shared account’s credentials.  Sharing credentials does not follow Best Practices and Muhimbi is not in control of this aspect of the way Flow works.

To make sure there are no authentication problems when deploying the Power App to end-users, we are using the REST API directly and using the API Key to authenticate instead of username and password.


Step 14:

Add the Parse JSON action.

  • Content: Add the “Outputs” the output of the “HTTP” action.
  • Schema: Click on “Generate from Sample” and paste the schema below.
{
"type": "object",
"properties": {
"result_code": {
"type": "string"
},
"result_details": {
"type": "string"
},
"base_file_name": {},
"processed_file_content": {
"type": "string"
}
}
}

Your final Parse JSON action should look like the screenshot below.


Step 15:

Add the ‘Send an email(v2)’ action.

  • To: Your email address.
  • Subject: Email Subject.
  • Body: Email Body.
  • Attachments Name – 1: Merged.pdf
  • Attachments Content -1: ‘processed_file_content’ the output of Parse JSON 2.

That’s it- you’re done! Launch your Power Apps, upload a few attachments and click on “Collection Attachment data” and “Trigger Merge Flow” button . After a short wait, you will find the email in your Inbox with the attachment(s) included.

Subscribe to this blog for the latest updates about SharePoint Online, Power Automate (Microsoft Flow), Power Apps and document conversion and manipulation using The PDF Converter.

One thought on “Merge Attachments from Power Apps Attachment Control and Send results via email

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s