Survey123 Tricks of the Trade: Working with attachments in Microsoft Power Automate (v2)

4460
11
01-17-2024 05:27 PM
IsmaelChivite
Esri Notable Contributor
10 11 4,460

 

Survey123 smart forms let you record signatures, photos, annotated images and upload documents into ArcGIS. All of these are stored in ArcGIS as 'attachments'. This article describes how you can use Microsoft Power Automate to work with these attachments. Specifically to:

  • Include one or more Survey123 attachments in an email
  • Upload Survey123 attachments into Microsoft OneDrive

I will assume you are already familiar with task automation using Survey123 and Microsoft Power Automate. If so, get started by publishing a survey with an image, signature or some other question type that uses ArcGIS attachments. Then log into Microsoft 365 and create a new Automated flow with the Survey123 trigger. Follow the instructions below to learn how to work with these Survey123 attachments in Microsoft Power Automate.

Adding multiple attachments to an email


Survey123 attachments are modeled as an array. In Microsoft Power Automate, you can create an array variable and populate it with Survey123 attachments. Later you can include that array variable in your email to define what attachments should be included with your message.

Immediately after your Survey123 trigger, add the Initialize variable action. We will populate this variable later with the attachments from your survey. Make sure you set the Type to Array and set a descriptive name. Leave the initial value empty.

IsmaelChivite_0-1705520802302.png

Next, add the Get attachment from URL action from the ArcGIS connector.  This will help you download the attachments. For the URL parameter, you will use the Dynamic content dialog and select the feature attachment URL for your question. In my case, the question with attachments in my survey is called Photos, so the dynamic content is feature attachments photo URL.

 

2024-01-17 Each.gif

 

As soon as the URL parameter is set, Microsoft Power Automate will wrap the Get attachment from URL action within an Apply to each block. This is because the Photos question in the survey may have one or more attachments in it. Power Automate cycles through the Survey123 attachments using an Apply to each block.

At this moment, we are ready to populate the Array variable we setup initially with the attachments found in our Survey123 Photos question.  This is done through the Append to array variable operation and since it is the most complex part of the entire workflow, so I broke it down into two steps.

Step 1: First we add the Append to array variable action right after Get attachment from URL, and within the Apply to each block. For the Name parameter, we will select the name of the array variable we want to populate, which is the one we created at the beginning of the flow.  The Value parameter must include a JSON string so we can specify both the name as well as the actual content of the file.

Step 1.gif

The preliminary JSON string looks like this: 

 

{
  "Name": <File name including the extension goes here> ,
  "ContentBytes": <File contents go here> 
}

 

Step 2: To specify the correct file name and contents, we will use Dynamic content. In my case, since the question in the survey with the attachments is called Photos, the name comes from feature attachments photos name. The ContentBytes value in the JSON needs to get populated with the Attachment contents output from the Get attachment from URL action.

2024-01-17 Step 2.gif

 

With the array populated with all the Survey123 attachments, we can now proceed to configure the Send Email action in Microsoft Power Automate.

Add a new Step to send an email, but make sure it gets added outside the Apply to each block. After completing the Recipient, Subject and Body parameters, expand the Advanced options

In the Advanced options you can specify the attachments that should be included in the email. Note that there is a switch button that lets you specify either a single, or multiple attachments. In our case, we want to select the multiple attachment view and then insert the value of the array we populated in there, as shown in the animation below.

2024-01-17 Send Email Configure Attachments.gif

 

Now you are set! Save and test. The workflow described above will work when you have one, but also when you have many attachments.

Adding Survey123 attachments to Microsoft OneDrive and Sharepoint

 

Next we will have a look at another common scenario where you might want to backup your files, or host them outside ArcGIS so they can be used with other third party systems.

If you want to upload your attachments into Microsoft One Drive, for example, you can use the Upload file from URL action. For the Source URL property, select from the Dynamic content dialog the feature attachments URL array for the appropriate question in your survey. Remember that if working against a private survey you will also need to add your token to the url (?token=[Portal Info Token]).

An apply to each block will be added automatically for you. Now all you need to do is to select the Destination File Path for your file. You can select an existing folder, or even create a new one.

IsmaelChivite_6-1705520072197.png

In the example above, I set the Destination File Path using one of the questions in my survey. The value of my question determines the directory in OneDrive where the file will be stored. If the folder exists, it will use it. Otherwise, it will create a new one. This is an interesting concept, as it helps organizing files nicely: by ZIP
code, by case ID, by field team, etc.

A similar approach can be used to upload files into SharePoint as shown in the following screenshot:

IsmaelChivite_0-1705541141843.png

When uploading photos into OneDrive or Sharepoint, you do not need to initialize an array variable at the beginning of the flow.

11 Comments