Hello! I have two layers— Trees and RemovedTrees— in a hosted feature layer and I'm trying to create in script in AGOL Notebooks (Python 3 Standard- 14.0) that will query the Trees layer for any trees marked as removed and copy them to the RemovedTrees layer.
I have it working except for the attachments, which I would also like to copy over. When I run the script, it seems to run successfully and if I go to the RemovedTrees layer the copied features will have an attachment, but there's no actual image.
I'm fairly new to Python, especially in the AGOL environment, and I've read everything I can find about attachments and I think I'm only more confused now.
I would appreciate any insight into how to accomplish this! Thank you!
Here is my code:
from arcgis.gis import GIS
gis = GIS("home")
trees_layers = gis.content.get('383df5bd920f4a6fb12cd475042923c7')
# Access the Trees layer
treeslyr = trees_layers.layers[0]
# Access the RemovedTrees layer
removedTreeslyr = trees_layers.layers[1]
# Create a FeatureSet of trees marked as removed from the Trees layer
treeslyr_fset = treeslyr.query(where= "Condition = 'Removed'", return_geometry=True)
#Create list of features from FeatureSet
feat_list = treeslyr_fset.features
#Variable to hold attachments for each feature
attachments_adds = []
for feature in feat_list:
source_guid = feature.get_value("GlobalID")
if not source_guid:
continue
source_oid = feature.get_value("OBJECTID")
if source_oid is None:
continue
source_attachments = treeslyr.attachments.get_list(oid=source_oid)
for att in source_attachments:
attachments_adds.append(
{
"globalId": att.get("globalId"),
"parentGlobalId": source_guid,
"name": att.get("name"),
"contentType": att.get("contentType"),
"attachmentId": att.get("id")
}
)
add_result = removedTreeslyr.edit_features(
adds=treeslyr_fset,
use_global_ids=True,
rollback_on_failure=False,
attachments={"adds": attachments_adds}
)
add_result
Trees and RemovedTrees have attachments enabled.
supportsApplyEditsWithGlobalIds is True.