Select to view content in your preferred language

CIM FieldDescriptions is not accessible for some layers

676
4
Jump to solution
09-26-2023 08:01 AM
SzymAdamowski
New Contributor III

Environment: ArcGIS Pro 3.1.2

Python CIM is a great way to modify field properties for a layer, as described here  (example 2 - Modify field properties). Among others, it seems like an easy way to turn field visibility on and off for layers in project without all the complication of using FieldInfo (example).

Well, at least in theory. In reality it turned out that FieldDescriptions array is not exposed in some cases. And the simplest scenario is just dragging any feature class into the map. Let's say we have point TestFeature in File Geodatabase which have standard fields OBJECTID, Shape and additional field called TestField.

Following code will not find any fields in this given scenario for the "fresh" layer:

 

 

m = arcpy.mp.ArcGISProject('current').activeMap
lyr = m.listLayers('TestFeature')[0] 
cim_lyr = lyr.getDefinition('V3') #Neither V2 nor V3 works
print (f"Number of fields: {len(cim_lyr.featureTable.fieldDescriptions)}")
for fd in cim_lyr.featureTable.fieldDescriptions:
    print (fd.fieldName) #should print fields names

 

 

And the result is:

 

 

Number of fields: 0

 

 

However, if I make any changes to layer Fields properties manually (by going to DataDesign -> Fields of TestFeature layer and making any changes and saving them) , FieldDescriptions are "fixed" and the result of above script is:

 

 

Number of fields: 3
OBJECTID
Shape
TestField

 

 

This behavior is also confirmed by analyzing saved layer files (lyrx, which in fact is JSON) before and after saving changes in DataDesign/Fields. Before saving JSON doesn't contain FieldDescriptions in CIMFeatureTable:

SzymAdamowski_0-1695739891609.png

After saving (any, even not real) changes in Fields (making sure that Fields editor is started from Layer, not from Feature Class)

SzymAdamowski_1-1695740009155.png

CIMFieldDescriptions appears in lyrx/JSON:

SzymAdamowski_2-1695740075319.png

1) Is there any Python workaround that would fix accessibility of  CIM FieldDescriptions for layers that don't have them? (What I've shown is manual workaround, which for many reasons is impractical in my case).

2) If it is a bug (I suppose so), could it be fixed in next ArcGIS Pro versions?

1 Solution

Accepted Solutions
Jennya
by
New Contributor

Well, if you add the bottom 2 lines after the AlterField, it works for me. 

arcpy.management.AlterField(l, field_name, field_name) 

l_cim = l.getDefinition('V2')
l.setDefinition(l_cim)

View solution in original post

4 Replies
PhilLarkin1
Regular Contributor

1) Is there any Python workaround that would fix accessibility of  CIM FieldDescriptions for layers that don't have them?

 

ESRI documentation promotes a hack:

If the layer does not have field descriptions, it calls the AlterField function on the first field, which forces the information into the CIM without making changes to the field.

 

arcpy.management.AlterField(lyr, lyrFld.name) #Force CIM update  

 

 

Seems that the efficiency gain from not generating this information is less important that offering a predictable developer experience from the CIM API. I wish fieldDescriptions was always there. 

PhilLarkin1
Regular Contributor

I should note this hacky solution doesn't seem to work at Pro 3.2.0 when run in a Script Tool.

0 Kudos
Jennya
by
New Contributor

Well, if you add the bottom 2 lines after the AlterField, it works for me. 

arcpy.management.AlterField(l, field_name, field_name) 

l_cim = l.getDefinition('V2')
l.setDefinition(l_cim)

SzymAdamowski
New Contributor III

Works for me as well (in Pro 3.2).Thx

0 Kudos