Select to view content in your preferred language

AttachmentManager.search() Issues with attachment_where parameter

267
1
02-19-2025 05:58 AM
Raul
by
Regular Contributor

Hi all, I'm experiencing issues using the search function from the AttachmentManager class. Currently I'm doing something like this:

 

item = gis.content.get(item_id)
fl = item.layers[0]

result = fl.attachments.search(where="1=1", attachment_where="ATT_NAME LIKE '%foto2_241023075432%'")

print(f"Attachments found: {len(result)}")

 

 

When I perform this query like this, the result is all  31684 attachments, so the attachment_where parameter is being ignored.

I performed the same query using the REST API, and it correctly returns the single attachment that matches my criteria.

Raul_0-1739973335188.png


My organization is using ArcGIS Enterprise 11.3, and I myself am using the gis api for python 2.4.0 (the one that comes preinstalled with arcgis pro. Currently using ArcGIS Pro 3.4.0)

0 Kudos
1 Reply
Raul
by
Regular Contributor

For anyone having a similar issue, I created this function that uses the REST API to perform the query. I'm still using my instance of FeatureLayer and GIS objects :^]

def __query_attachments_workaround(self, featurelayer, where=None, attachment_where=None):
	"""
	Performs the QueryAttachments operation using the GIS REST API
	Parameters:
		featurelayer (object): Instance of arcgis.features FeatureLayer class where to query attachments
		where (string | None): String representing the definition expression to be applied to FEATURES when performing query
		attachment_where (string | None): String representing the definition expressionm to be applied to ATTACHMENTS when performing query
	Returns:
		response_list (list): List of objects representing the server response to queryAttachments operation.
		The response has been formatted so it's similar to the native featurelayer.attachments.search() response
	"""
	import requests
	import json
	import sys
	try:
		url = f"{featurelayer.url}/queryAttachments"
		token = self.gis._con.token

		params = {
			'definitionExpression': where if where else "1=1",
			'attachmentsDefinitionExpression': attachment_where if attachment_where else '1=1',
			'f': 'json',
			'token': token
		}

		response = requests.post(url, params=params)
		if(response.status_code != 200):
			raise requests.RequestException('Invalid Server Reponse')

		response = json.loads(response.content)['attachmentGroups']
		response_list = []
		for record in response:
			response_list += [{
				'PARENTOBJECTID': record['parentObjectId'],
				'PARENTGLOBALID': record['parentGlobalId'],
				'ID': x['id'],
				'ATTACHMENTID': x['ATTACHMENTID'],
				'GLOBALID': x['GLOBALID'],
				'NAME': x['ATT_NAME'],
				'CONTENTTYPE': x['CONTENT_TYPE'],
				'SIZE': x['DATA_SIZE'],
				'DOWNLOAD_URL': f"{featurelayer.url}/{record['parentObjectId']}/attachments/{x['ATTACHMENTID']}?token={token}"
			} for x in record['attachmentInfos']]
		return response_list

	except Exception as e:
		print('An unhandled exception has occurred while querying for attachment infos')
		print(f"Details: {repr(e)}")
		sys.exit(5)

 

Note: this function currently belongs to a class that contains an instance to my GIS object, modify the code accordingly if you're going to use it in your environment.

Hope the issue in the API gets solved soon tho!

0 Kudos