Export Attachments Toolbox

13902
19
06-25-2021 02:33 AM
UriGilad_EsriAu
Esri Contributor
42 19 13.9K

 

The Export Attachments Toolbox provides an easy solution for exporting attachments from a feature service on AGOL/Portal or from a local Geodatabase to a local folder. The Toolbox offers various export options such as distributing the exported attachments into multiple folders according to attachment attributes, adding a prefix to attachment name, and downloading the feature service from AGOL/Portal as a File Geodatabase.

If you like this toolbox I'd appreciate if you give a Kudos. Thanks! 👍

 

Background

 

Attachments, in particular photos, are a common component in spatial data capture and management. As such, Esri’s feature classes (in Desktop environment) and hosted feature services (in AGOL/Portal environment) can be enabled to store attachments relating to individual features. The attachments are saved as Blobs in a related table, pointing to the parent feature class through a Relationship Class.

While it is possible to batch upload attachments to a feature class by using the Add Attachments Tool, the management of existing attachments in a feature class is limited:

  • Individual attachments can only be viewed in dedicated applications, for example Survey123 Online data viewer, or configured pop-ups in web maps, apps, and ArcGIS Pro.
  • Batch downloading of attachments is possible though external software such as FME, or setting up a limited script which has to be applied separately to each attachment table relating to the feature class. Manual downloading of the data is required if the layer is on AGOL.
  • Large or multiple attachments increase the feature class size quickly making it bulky and less responsive.
  • Backup and sharing of attachments are cumbersome as attachments are ‘hidden’ in a feature class, and easy access is only possible through GIS platforms.

Consequently, many users request a dedicated tool that will allow the easy export of attachments from the feature service to a local folder where they can be managed independently.

 

Export Attachments Toolbox

 

The Export Attachments Toolbox facilitates the exporting of attachments from Geodatabases or AGOL/Portal hosted feature services.

 

Tools

 The toolbox has three tools:

  1. Export All Attachments from Database Extracts all attachments from a FGDB or EGDB into a local folder. Attachments can be assigned with various file name prefix options such as parent feature class name, related field value, or a text prefix provided by the user.
  2. Export Attachments from Local Table Based on this Esri support page, this tool extracts all attachments from a single attachment table to a local folder. It is similar to the first tool but applies only to one table at a time and does not include advanced options.
  3. Export Hosted Feature Layer & Attachments This tool offers several options:
    1. Only FGDB: Download a hosted feature service from AGOL/Portal to a local folder as a File Geodatabase.
    2. Only Attachments: Extract all attachments from an AGOL/Portal hosted feature service to a local folder without downloading the hosted feature service.
    3. FGDB + Attachments: Download a hosted feature service from AGOL/Portal and extract all its attachments to a local folder.

 

Grouping Attachments

When downloading attachments from a geodatabase or a hosted feature service, the user can group the attachment in separate folders based on the related feature attributes. For example, in an asset feature class with asset type associated to each attachment, the tool will download all attachments related to asset x in folder x and all attachments related to asset y in folder y, etc.

The specified group name must be entered by the user in the exact way it appears in the feature class. If the group name is not found as a field in the feature class, the attachments will be downloaded to a folder named ‘No Group Assigned’.

 

Prefix Options

When downloading attachments, the user can specify an attachment prefix that will be added to the attachment’s name. The prefix can be one of the following:

  1. No Prefix – No prefix will be added to attachment name
  2. Type Prefix – A prefix added by the user to the attachment name. For example, images that only have a date in their name can be added a fieldwork_ prefix to be named fieldwork_imagedate.jpg.
  3. Group Name (only available for grouped attachments, see Grouping Attachments section for more info) – The group name will be added as prefix to attachment name. For example, if the attachments are grouped by asset type, an attachment named 2020101.jpg related to a Streetlight asset type will be saved as Streetlight_20200101.jpg.
  4. Group Name + Type Prefix (only available for grouped attachments, see Grouping Attachments section for more info). The group name will be added as prefix to attachment name, and the user’s prefix will follow. For example, an attachment named 2020101.jpg related to a Streetlight asset type with a user prefix of _fieldwork_ will be saved as Streetlight_fieldwork_20200101.jpg.
  5. Related Feature Class Name – The name of the feature class that holds the attachment will be added as a prefix. For example, if the parent layer name is Brisbane_Assets and the attachment is 20200101.jpg the attachment will be saved as Brisbane_Assets_20200101.jpg.

 

Toolbox and Scripts

 

Attached in this blog is a version of the toolbox with the scripts embedded ('imported'). To view the script, right click on the script in Pro and click 'Edit'.

 

Technical Considerations

 

AGOL/Portal Access

To use the Export Hosted Feature Layer & Attachments tool make sure you have the appropriate permissions to download hosted feature services or attachments from AGOL/Portal. If you cannot see the ‘Export’ button in the service’s overview page in your AGOL you will not be able to download it with the tool.

If your Portal has a proxy you will need to provide the tool with proxy host name and port number.

Environments

The toolbox was created in an ArcGIS Pro 2.6.3 / Python 3.6.10 environment. For best results, run the tools in a similar or upgraded environment. Using previous versions, especially Python 2 versions, is likely to cause the toolbox to encounter errors.

 

Terms of Use

 

Support and Upgrades

This toolbox is not an official Esri toolbox. It is therefore not supported or updated. 

Published by Uri Gilad, 25 June 2021

 

19 Comments
MehdiPira1
Esri Contributor

Nice! Very useful @UriGilad_EsriAu 

CatherineCallahan
New Contributor

Thanks for sharing this toolbox @UriGilad_EsriAu

Bnewell
New Contributor

Thanks @UriGilad_EsriAu, this is very helpful! 

Ursu_Lacramioara
New Contributor III

Hi Uri - I am trying to use your script but I get an error with the AGOL portal I am using. 

if i take the/0 from hosted then the Portal is fine but when I use /0 then the Portal gives an error.

any idea? 

Ursu_Lacramioara_0-1647625872933.png

Thanks

Lacri

UriGilad_EsriAu
Esri Contributor

Hi Larci,

In the Hosted Item ID you only need to insert the 32-character code, not the entire URL.

Cheers,

Uri

 

benjamin_green
New Contributor

I found this toolbox extremely useful when downloading photo attachments from Survey123. Thank you!

I ran into some initial difficulties when exporting the attachments. I received an error message:

NameError: name 're' is not defined

After some research on Stack Overflow (link to relevant article here), I found the solution. You need to import regular expression module in the code for this tool. Adding it to the first block of code when importing modules will do the trick. 

import re 

 Hope this helps anyone else who may run into this issue. Cheers.

JoeBryant1
Occasional Contributor III
This tool MIGHT be awesome, if I can figure out this error:
 
Start Time: Thursday, August 18, 2022 4:21:26 PM
['Asset_Inventory__ATTACH', ['{83455E31-D2A8-49FC-80B3-B0398D23545D}', '{83455E31-D2A8-49FC-80B3-B0398D23545D}', '001 South Cordelia  Zone 1 Pump Station'], ['{83455E31-D2A8-49FC-80B3-B0398D23545D}', '{83455E31-D2A8-49FC-80B3-B0398D23545D}', '001 South Cordelia  Zone 1 Pump Station'], ['{83455E31-D2A8-49FC-80B3-B0398D23545D}', '{83455E31-D2A8-49FC-80B3-B0398D23545D}', '001 South Cordelia  Zone 1 Pump Station']]
Traceback (most recent call last):
  File "<string>", line 173, in <module>
NameError: name 'grouping_value' is not defined
Failed script Export All Attachments from Local Database...
Failed to execute (ExportAllAttachmentsFromLocalGDB).
Failed at Thursday, August 18, 2022 4:21:45 PM (Elapsed Time: 19.77 seconds
 
I am using your 'Group Attachments By...' method and have entered a field for the facility the picture was taken in. This field is completely populated (no null values), and is able to be used for a few records, but the tool seems to fail when the "grouping value is not defined". Could this be because of unsupported characters like '#' or '()' not getting passed by the compiler?
 
This tool is almost there - the Grouping method is (or will be) exactly what I need. Thanks for developing this!
JoeBryant1
Occasional Contributor III

Update: I found that the grouping field I was using had a coded value domain, even though it was showing the descriptions. The coded values where also text, but they started with numbers, which I believe is a no-no for Windows folder names. I calculated decoded string values to a new field and used that field for grouping, and the tool ran successfully.

SheldonBina2
New Contributor II

Great tools!  Thanks for providing!

CherieMalone
New Contributor

Hi Uri! Great tools, thank you 🙂

Is there a way to export selected records or records based on a particular attribute from a hosted feature layer?

MattRummel
New Contributor II

Cannot get this to work in ArcGIS Pro 3.0. Keeps returning the error:

 

Traceback (most recent call last):
File "<string>", line 151, in <module>
RuntimeError: A column was specified that does not exist.

LaineyLe_Blanc1
New Contributor III

It would be great if there was an option to set the prefix of the attachment name to the Global ID, so that the downloaded picture and data point can be related. I've seen in older versions of this tool that you could set the Feature ID to the related table ID to export attachments, as shown in this how-to https://www.youtube.com/watch?v=Fw1K8rmV8O4

JohnWenck
New Contributor II

I'm having the same grouping issue as MattRummel.  It's not recognizing the field name that I want to group the photos by.  I tried both option 1 from local database and option 3 from hosted feature service and got the same error.

MattRummel
New Contributor II

Hi John Wenck - 

My solution: I had two feature datasets within the same geodatabase, each had their own set of attachments. I am guessing this architecture caused the confusion, because I ended up exporting the features from each feature dataset into their own geodatabases, and the tool worked. Hope this helps, Matt

CynFadem
New Contributor

Are there any updates for this toolset? It's super great, but seems to have errors, at least in 3.0. Thanks!

JenLangevin
New Contributor III

Great tool! The export attachments from a table works great, BUT is there any way I can set it to no prefix? I have already named my photos and do not want them to have an additional prefix. 

Thanks,

-Jen 

Jen_Zumbado-Hannibal
Occasional Contributor

This script didn't work on ArcGIS Pro 3.1.3. I suppose it's mostly because the modules in Python 3.X are now different. It would be nice to have it upgraded 🙏

However, I did find a cool video using FME tools How Do You Get 30K Photo Attachments Out of ArcGIS Enterprise or ArcGIS Online? - YouTube, for those that have a Data Interoperability extension. I don't usually work with this extension so I didn't understand all the nuances. Yet, I followed the steps from the video and it extracted the photo attachments from my hosted feature layer from Portal. 

It did take me all day to figure it out 😵. I'm still struggling to create the Excel spreadsheet 😣

If someone does figure the spreadsheet out...poke me and let me know. 

Spread the knowledge! 

chill_gis_dude
New Contributor III

Very cool tool. I almost got it working but am stuck with the message: 

Capture.PNG

chill_gis_dude
New Contributor III

Has anyone gotten this to work for them lately?

I am getting error messages from the tool script: Capture.PNG