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:
After saving (any, even not real) changes in Fields (making sure that Fields editor is started from Layer, not from Feature Class)
CIMFieldDescriptions appears in lyrx/JSON:
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?