Code - Does Feature Class have attachments

1023
11
Jump to solution
03-01-2023 05:41 AM
kapalczynski
Occasional Contributor III

Looking for some code that would verify if a Feature Layer has Attachments...

I can check using the describe for many things like if Tracking is Enabled but not sure for attachments I do not see that as a child of DESCRIBE

Thoughts... 

 

 

desc1 = arcpy.Describe(LayerA)
hasEditorTracking = desc1.editorTrackingEnabled
print(hasEditorTracking )

 

 

 

0 Kudos
1 Solution

Accepted Solutions
Clubdebambos
Occasional Contributor III

You can also use ArcPy

 

import arcpy

## Path to Feature Class in SDE/GDB
fc = r"path\to\fc"

## get a describe dictionary
desc = arcpy.da.Describe(fc)

## if there is an ATTACHREL relationship class print the name of it.
## this would hint that attachments are enabled
for relclass_name in desc["relationshipClassNames"]:
    if "ATTACHREL" in relclass_name:
        print(relclass_name)
~ learn.finaldraftmapping.com

View solution in original post

0 Kudos
11 Replies
Clubdebambos
Occasional Contributor III

Hi @kapalczynski

See commented code below for checking if a layer has attachments enabled for a Feature Layer. 

from arcgis.gis import GIS

## connect to AGOL
agol = GIS("home")

## access Feature Service Item using Item ID
item = agol.content.get("INSERT_ITEM_ID")

## iterate through layers and print name and if hasAttachments (True or False)
for lyr in item.layers:
    print(lyr.properties.name)
    print(lyr.properties.hasAttachments)

 

 

~ learn.finaldraftmapping.com
0 Kudos
kapalczynski
Occasional Contributor III

OK I should have been a bit more specific... this data resides on our oracle server and is publish to portal.  I would assume that it would change the syntax a bit?

Where this is referencing AGOL

item = agol.content.get("INSERT_ITEM_ID")

 

0 Kudos
Clubdebambos
Occasional Contributor III

Hi @kapalczynski 

Same principal applies for AGOL/Portal, you can get the feature service item id from the URL when you click into the service item in Portal. Just copy the id into the script. 

Clubdebambos_0-1677681758032.png

GIS("home") will access AGOL or Portal depending on which one you are logged into last in ArcGIS Pro.

You could change this to use full parameters

agol = GIS("PORTAL_URL", "PORTAL_USERNAME", "PORTAL_PASSWORD")

 

~ learn.finaldraftmapping.com
Clubdebambos
Occasional Contributor III

You can also use ArcPy

 

import arcpy

## Path to Feature Class in SDE/GDB
fc = r"path\to\fc"

## get a describe dictionary
desc = arcpy.da.Describe(fc)

## if there is an ATTACHREL relationship class print the name of it.
## this would hint that attachments are enabled
for relclass_name in desc["relationshipClassNames"]:
    if "ATTACHREL" in relclass_name:
        print(relclass_name)
~ learn.finaldraftmapping.com
0 Kudos
kapalczynski
Occasional Contributor III

Thats what I was looking for @Clubdebambos  

I wont know the ID unless I look for it... need this to be within Python....

THANK YOU BOTH for your thoughts...

 
0 Kudos
kapalczynski
Occasional Contributor III

I do get this error though:

Traceback (most recent call last):
File "C:/Users/Ja/Desktop/GIS_projects/Land Use Project Folder/PythonScripts/LUPS_Data_TEST_to_UAT_DESCRIBE.py", line 47, in <module>
for relclass_name in desc1["relationshipClassNames"]:
TypeError: 'geoprocessing describe data object' object is not subscriptable

 

 

LayerA_Tst = "Database Connections\\TEST@gis_data.sde\\GIS_DATA.SDE_TEST"
desc1 = arcpy.Describe(LayerA_Tst)

for relclass_name in desc1["relationshipClassNames"]:
    if "ATTACHREL" in relclass_name:
        print(relclass_name)

 

0 Kudos
Clubdebambos
Occasional Contributor III

I'm using the data access module in mine to access describe as a dictionary rather than a describe object. Change to this line below.

desc1 = arcpy.da.Describe(LayerA_Tst)
~ learn.finaldraftmapping.com
kapalczynski
Occasional Contributor III

Thanks again... missed that...

kapalczynski
Occasional Contributor III

question:  If there are no Attachments there it does nothing... I cant seem to trap if they are there if they are not...

If there are NO attachments I don't get True OR False

 

for relclass_name in desc1a["relationshipClassNames"]:
    if "ATTACHREL" in relclass_name:
        print("True")
    else:
        print("False")

 

 

EDIT:  I guess I can just play with a variable and change it from True to false and report on that.... I think I can get it to work.... albeit clumsy..

0 Kudos