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

28551
94
02-25-2021 07:58 PM
IsmaelChivite
Esri Notable Contributor
9 94 28.6K

 

This content is obsolete.

Check this new post for the latest.

 

Survey123 smart forms let you record signatures, photos, annotated images and upload documents into ArcGIS. All of these are stored in ArcGIS as 'attachments'. Microsoft Automate includes a Survey123 connector that helps you access ArcGIS attachments submitted through a smart form.


This blog provides guidance for two common scenarios involving Survey123 attachments and Microsoft Power Automate:

  • Adding multiple Survey123 attachments to an email
  • Uploading Survey123 attachments into Microsoft OneDrive

It is assumed 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, file or some other question that uses ArcGIS attachments. Then log into Office 365 and create a new Automated flow with the Survey123 trigger. Follow the instructions below to experiment first hand with Survey123 attachments in Power Automate.

Before we start

 

To work with ArcGIS attachments in Power Automate, we are going to use the HTTP module in Power Automate. Through this module we will query your survey's layer using the ArcGIS REST API. Now, for these queries to work, you first need to make sure that query capabilities are enabled in your survey's layer.

The easiest way to enable queries in your survey layer is though the Collaborate tab in the Survey123 website, although you can also do it manually through the item details page of your survey's layer.

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.

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 action Type to Array and set a descriptive name. Leave the initial value empty.

Automate_001.png

Next, add an HTTP action. This will help us download the attachments. Set the Method to POST. For the URI, 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 photo, so the dynamic content is feature attachments photo URL.

If you are working with a survey that is shared privately, you will also need to add your token as a query parameter. You can get a valid token from the dynamic content dialog (Portal Info Token).

Automate_002.png

Survey123 attachments URLs are modeled as an array because a single question in Survey123 can be used
to capture one or many attachments. Microsoft Automate will recognize your photo URL content as
an array and add an Apply to each block automatically.


Immediately after your HTTP module, add an Append to array variable action.

  • For the Name, select the variable you initialized at the top of your flow.
  • For the value, you need to carefully enter a JSON string specifying the ContentBytes and unique Name of the attachments.
  • Start by adding this JSON string

{
  "ContentBytes": <Your HTTP action Body goes here. Build using an Expression>,
  "Name":<Your feature attachments name goes here. Get from Dynamic Content>
}

  • Now, replace the ContentBytes and Name values as follows:
    • ContentBytes: Use the Expression tab to write the following expression: base64ToBinary (body('HTTP').$content)
    • Name: Use the Dynamic content tab to select the feature attachments name property for the question with your attachments. In my case it was feature attachments photos name.

fow3.gif Make sure you set your JSON value accurately. Do not forget the commas, curly brackets and other things that make JSON be JSON...  Do not forget the comma right after the Body Content!

Add a new Step to send an email.

Automate_004.png

 The key to setting up the Send an email action is to open the Advanced options panel and click the Switch to input entire array icon.

 

Automate_005.png

 You will now be able to use the Dynamic content dialog to select the array variable you setup at the beginning of the flow.

Automate_006.png

Adding Survey123 attachments to Microsoft OneDrive

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.

Automate_007.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, Google Drive or Microsoft Azure File Storage

 

94 Comments