Select to view content in your preferred language

Code - Does Feature Class have attachments

2909
12
Jump to solution
03-01-2023 05:41 AM
kapalczynski
Frequent Contributor

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
12 Replies
kapalczynski
Frequent Contributor

I just did this with a couple variables:

 

hasAttachments1 = "FALSE"
hasAttachments2 = "FALSE"

for relclass_name in desc1a["relationshipClassNames"]:
    if "ATTACHREL" in relclass_name:
        hasAttachments1 = "True - " + str(relclass_name)
        
for relclass_name in desc2a["relationshipClassNames"]:
    if "ATTACHREL" in relclass_name:
        hasAttachments2 = "True - " + str(relclass_name)

print("---Attachments: " + hasAttachments1)
print("---Attachments: " + hasAttachments2)
0 Kudos
Clubdebambos
MVP Regular Contributor
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 are no relationships classes then no attachments
if not desc["relationshipClassNames"]:
    print("FALSE")
## if there are relationship classes
else:
    ## check if there is an ATTACHREL relationship class
    if [relclass_name for relclass_name in desc["relationshipClassNames"] if "ATTACHREL" in relclass_name]:
        print("TRUE")
    ## otherwise no attachments
    else:
        print("FALSE")
~ learn.finaldraftmapping.com
0 Kudos
PaulHaakma
Esri Contributor

Here's another way to achieve this using one line.

Using the Data Access describe ensures we have a dictionary and can use the 'get' method to provide a fallback of an empty list if the relationshipClassNames key doesn't exist.

The code iterates over the strings in the relationshipClassNames list and checks if any match the baseName of the featureclass with '__ATTACHREL' at the end which indicates attachments are enabled.

The python 'any' keyword returns true if any of the strings match and false if none do.

This should therefore return true if there are attachments and false if not. It's essentially the same as previously suggested code, but some people prefer one liners and some prefer multiline if/else statements as more readable. Take your pick  😀

 

desc = arcpy.da.Describe(feature_class)
hasAttachments = any(value == f'{desc.get('baseName', '||')}__ATTACHREL' for value in desc.get('relationshipClassNames', []))

 

0 Kudos