Select to view content in your preferred language

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

8572
21
01-17-2024 05:27 PM
IsmaelChivite
Esri Notable Contributor
11 21 8,572

 

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.

21 Comments
Tiff
by
Frequent Contributor

EDIT - Resolved this by just putting the variable in the box and it worked perfectly. 

Thank you @IsmaelChivite for this detailed workflow. In the Advanced settings step of the email, I am seeing something different from your single "Incident Photos" variable. It is asking me for the same json format as the append to array variable action as soon as I press the button "Switch to input entire array". Can you please assist?

Can I put the dynamic content Photos variable in place of the json? It says "Attachments Item" where yours says "Attachments" so I am not sure if that is the same.

Tiff_0-1706101554562.png

Tiff_1-1706101567822.png

 

 

AndrewThompson_SCE
Emerging Contributor

I have jpg files downloading but there is no photo. Its all 2KB files. Do we need to put a token in the photo url for it to download correctly? 

 

AndrewThompson_SCE_1-1706293932567.png

 

AndrewThompson_SCE_0-1706293894722.png

 

Edit:  Adding the token to the URL worked. 

 

abureaux
MVP Frequent Contributor

Can't wait to try this. This has been on my list of things to do for way too long now.

Thanks again @IsmaelChivite for the detailed guide!

Tiff
by
Frequent Contributor

Hello @IsmaelChivite, question about flows where photos are optional. The photo field I am using in the "For each" trigger sometimes does not have a photo. I used the "Run after" setting to bypass this and send out an email regardless of if a photo is included. I am still getting "Flow failed" notifications as a result. Is my approach the best way to do this or should I do a conditional of some sort? I would ideally not like to be notified of a failed flow when the email does successfully send. 

Thank you! Please see screenshot example below.

Tiff_1-1707764048766.png

 

 

RobertAnderson3
MVP Regular Contributor

Hi @Tiff 

Try setting up using a Condition to check if the variable for the attachments is empty or not, then run the apply to each only if it does have content. If null, just send the email, if not null then run the apply to each.

RobertAnderson3_0-1707766334566.png

 

Tiff
by
Frequent Contributor

Thank you @RobertAnderson3! What I was missing from this was simply being able to put null down as the function/criteria. I didn't know I could do that. I've tried this and will be able to test it out soon.

RobertAnderson3
MVP Regular Contributor

No problem @Tiff! Yeah, it needs to be added in under the Expression, it's a function in there. Hopefully it works for you!

SusanMathai
Occasional Contributor

@IsmaelChivite   

Hi Ismael , Please can you identify the invalid JSON error here, regardless of whatever item i choose from the list as shown in the picture below, I get an error "Enter valid json"

 

SusanMathai_0-1710523342984.png

 

SusanMathai_0-1710523657666.png

 

I appreciate all your help!

Thanks a lot.

 

 

 

RobertAnderson3
MVP Regular Contributor

@SusanMathai It looks to me like you might have an extra comma in there beside the Name: section?

SusanMathai
Occasional Contributor

@IsmaelChivite   

 

Never mind I fixed the error it works perfect now.

EricBongelli
New Contributor

@SusanMathai and @IsmaelChivite 

 

What was your solution for the invalid JSON error? My flow used to work (as recently as March 2024), but recently stopped working. I have not changed anything in my flow, but I do now get the invalid JSON error on the Get URL attachment step. Any ideas would be appreciated.

EricBongelli_0-1712976291507.png

 

EricBongelli_1-1712976325818.png

 

 

OussamaMerhi
Occasional Contributor

Now I want to add the attachment to another feature layer using add attachment. How can i  do it

m3
by
Frequent Contributor

I'm having the same issue as @EricBongelli 

 

abureaux
MVP Frequent Contributor

@m3refresh your connections.

There is currently a bug that Esri is working on that may be causing you additional issues:

BUG-000169725 Authentication to ArcGIS Online through ArcGIS Connectors for Power Automate workflows frequently fails with the error message, "Authentication failed. Your session has expired."

m3
by
Frequent Contributor

This is a brand new connection, I can try refreshing it or creating another but I get the error still so I guess I'll wait on the bug. 

abureaux
MVP Frequent Contributor

That bug is only "In product plan" and the timeout is every hour.

Can you post a screen cap of the error?

If it is the same as EricBongelli and the "innerError" looks like this, that means the connection times out (like I mentioned above). In that case, refreshing the connection is the solution.

abureaux_0-1723668767212.png

If this isn't you error message, there may be something else going on.

First thing would be to check account permissions.

If account permissions aren't an issue (e.g. you are using an Admin account), then maybe try selecting the layer you want to work with in your Portal and "Add to favourites". Then, back in Power Automate, change "My Content" to "My  Favourites" and try again. I sometimes get better results that way.

m3
by
Frequent Contributor

"innerError": "Authentication failed. Your session has expired. Please log in again to continue."

That is my error message

abureaux
MVP Frequent Contributor

I would definitely try refreshing the connection.

  1. Click Connections
  2. If you are using Enterprise, look for the purple ArcGIS Enterprise connector.
    If you are using AGO, look for the orange ArcGIS connector.
  3. Click the ellipses (...)
  4. Click Switch account and log in using an account with appropriate permissions.

abureaux_1-1723669973777.png

After that, try the flow again. Honestly, it should work after this. If it still doesn't work, jumping on a call with Esri Tech Support may be a good next step so they can see exactly what you have configured.

m3
by
Frequent Contributor

That worked, I had to delete all my cookies to ensure I was switching to the correct account.

Thanks!

SaadShahzad
Emerging Contributor

I've tried everything to get this method to work, but I cannot get it to work for ArcGIS Enterprise and using attachments in a repeat. 

Any tips on how to get this working when attachments are put in a repeat? 

 

abureaux
MVP Frequent Contributor

@SaadShahzadyou need to loop through the repeat. While not working with attachments, I have a guide for working with nested repeats in Power Automate. The concepts are all the same though.

The only thing you will probably need to account for is empty/null attachments. That can be achieved with a "Condition" action.