I'm using the attachment viewer instant app as a demo run for some field software to enable field crews to document and organize photo locations. I've built out a quickcapture app that writes to a single hosted feature layer, prompting for an "ID" and "Note" field.
This functionality works well, field crews can log onto AGOL and see their geolocated photos on a map, then individually download the pictures from the instant app.
I'd like to build out a way for the users to mass download all attachments based on either a selection, or a filter. Currently I have to export the hosted feature layer to a gdb, bring it into ArcGIS Pro, and run the export attachments geoprocessing tool.
Is there a way to enable the functionality of the Export Attachments tool to AGOL? Whether it's in a notebook, or experience builder application?
Thank you!
In AGOL Notebooks, you could do something like this:
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
import os
import zipfile
# Connect to AGOL
gis = GIS("home")
# Access the hosted feature layer
feature_layer_item_id = "YOUR_FEATURE_LAYER_ITEM_ID" # Replace with your layer's item ID
layer_index = 0
flayer = gis.content.get(feature_layer_item_id).layers[layer_index]
# Client adjusts this filter as needed
where_clause = "County = 'Baltimore'"
# Query features
features = flayer.query(where=where_clause, return_geometry=False).features
# Temporary folder to hold attachments
temp_folder = "attachments_temp"
os.makedirs(temp_folder, exist_ok=True)
# Download attachments
for feature in features:
object_id = feature.attributes["OBJECTID"]
attachments = flayer.attachments.get_list(object_id)
for att in attachments:
att_id = att['id']
att_name = att['name']
att_content = flayer.attachments.download(object_id, att_id)
save_path = os.path.join(temp_folder, f"{object_id}_{att_name}")
with open(save_path, "wb") as f:
f.write(att_content)
# Create a zip file of all attachments
zip_filename = "Filtered_Attachments.zip"
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(temp_folder):
for file in files:
file_path = os.path.join(root, file)
zipf.write(file_path, arcname=file)
print(f"All attachments zipped into {zip_filename}")Might need some tweaking.
Also they'll need an AGOL login to create a notebook.
James, really appreciate the effort you put into this. I'm going to play around with the code this week and see where we land. Thanks again.