Feature layers support adding attachments to features, making them very useful during field data collection. On feature layers that support attachments, accessing attachments
property, will return an AttachmentManager
object. This object can be used to list, download, add, delete and update attachments.
# connect to web GIS
from arcgis.gis import GIS
gis = GIS("portal url", "username","password")
search_result = gis.content.search("Chennai Rainfall", "Feature Layer")
chennai_rainfall = search_result[0]
#get feature layers from the item
cr_lyr = chennai_rainfall.layers[0]
List attachments
Use the get_list()
method on attachments
property of a FeatureLayer
object to get a list of dictionaries containing information about attachments. The get_list()
accepts object id as parameter. You can loop through all the features and call this method if you would like to view all attachments on the layer
cr_lyr.attachments.get_list(oid=1)
[{'contentType': 'image/png', 'id': 1, 'name': 'AppTemplate.png', 'parentObjectId': 1, 'size': 1394}, {'contentType': 'image/png', 'id': 2, 'name': 'AppTemplate.png', 'parentObjectId': 1, 'size': 1394}, {'contentType': 'image/png', 'id': 4, 'name': 'AppTemplate.png', 'parentObjectId': 1, 'size': 1394}]
Download attachments
Use the download()
method on attachments
property of a FeatureLayer
object to download individual attachments. It accepts the object id of the feature and the attachment id of the attachment as parameters. If the save_path
parameter is not specified, the API saves it to user temporary directory
cr_lyr.attachments.download(oid=1, attachment_id=1)
'C:\\Users\\rohit\\AppData\\Local\\Temp\\AppTemplate.png'
Add new attachments
Use the add()
method on attachments
property of a FeatureLayer
object to add new attachments to a feature. It accepts the object id of the feature and path to attachment as parameters
cr_lyr.attachments.add(1, 'C:\\Users\\rohit\\AppData\\Local\\Temp\\AppTemplate.png')
{'addAttachmentResult': {'globalId': None, 'objectId': 5, 'success': True}}
You can list the attachments to verify the add
operation
cr_lyr.attachments.get_list(1)
[{'contentType': 'image/png', 'id': 1, 'name': 'AppTemplate.png', 'parentObjectId': 1, 'size': 1394}, {'contentType': 'image/png', 'id': 2, 'name': 'AppTemplate.png', 'parentObjectId': 1, 'size': 1394}, {'contentType': 'image/png', 'id': 4, 'name': 'AppTemplate.png', 'parentObjectId': 1, 'size': 1394}, {'contentType': 'image/png', 'id': 5, 'name': 'AppTemplate.png', 'parentObjectId': 1, 'size': 1394}]
Delete attachments
Unwanted attachments can be removed by calling the delete()
method on attachments
property of a FeatureLayer
object. It accepts the feature's object id and attachment's id as parameters
cr_lyr.attachments.delete(1,4)
{'deleteAttachmentResults': [{'globalId': None, 'objectId': 4, 'success': True}]}
List the attachments to verify removal
cr_lyr.attachments.get_list(1)
[{'contentType': 'image/png', 'id': 1, 'name': 'AppTemplate.png', 'parentObjectId': 1, 'size': 1394}, {'contentType': 'image/png', 'id': 2, 'name': 'AppTemplate.png', 'parentObjectId': 1, 'size': 1394}, {'contentType': 'image/png', 'id': 5, 'name': 'AppTemplate.png', 'parentObjectId': 1, 'size': 1394}]