HOW TO

Find features without attachments in a Portal for ArcGIS item using ArcGIS API for Python

Last Published: March 9, 2023

Summary

In Portal for ArcGIS, the Enable Attachments option can be set in a hosted feature layer to allow attaching images or files to individual features, such as attaching images to point features representing road incidents or survey locations. However, for larger datasets, it is time-consuming to manually verify if each feature contains an attachment.

The Python script provided in this article can be used to find the Object IDs of features without attachments in a Portal for ArcGIS item. In this example, the Layer_2_polygon hosted feature layer contains two features without attachments.

The attribute table of a hosted feature layer with three features listed.

Procedure

  1. Import the necessary modules.
from arcgis import GIS
  1. Specify the credentials to connect to the portal.
gis = GIS('<portal_URL>','<username>','<password>',verify_cert=False)
  1. Specify the item ID of the item to be queried.
svc = gis.content.get('<portal_itemID>')
  1. Create a loop to iterate through all items to check if the features contain attachments. Use the get_list() method to get a list of dictionaries containing information about the attachments.
lyr = svc.layers[0]
no_attach = []
oids = lyr.query(where='1=1', return_ids_only=True)['objectIds']
for n in range(len(oids)):
    oid = oids[n]
    attach = lyr.attachments.get_list(oid)
    if len(attach) == 0 :
        no_attach.append(oid)
Note:
To return Object IDs of features with attachments, replace "len(attach) == 0" with "len(attach) > 0".
  1. Print the result using the print() method.
print(no_attach)

The code block below demonstrates the full script.

from arcgis import GIS
gis = GIS('https://machines.esri.com/portal', 'Username1', 'Password1', verify_cert=False)

svc = gis.content.get('Layer_2_Polygon_itemID')
lyr = svc.layers[0] 
no_attach = [] 
oids = lyr.query(where='1=1', return_ids_only=True)['objectIds'] 
for n in range(len(oids)): 
    oid = oids[n] 
    attach = lyr.attachments.get_list(oid) 
    if len(attach) == 0 : 
        no_attach.append(oid)

print(no_attach)

The Object IDs of features without attachments are returned after running the script.

The Python window in ArcGIS Pro displaying the result after running the Python script.

Article ID:000029090

Software:
  • Portal for ArcGIS
  • ArcGIS API for Python 1 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic