I am upgrading a script that heavily relied on the arcpy.mapping package to work with ArcGIS Pro. I have gotten everything working okay EXCEPT for updating the visible fields in the map layers. I have tried a bunch of different suggestions, but none that have worked. The simplest approach seems to be using Python cim access: https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/python-cim-access.htm
My code runs okay, but when I open my .aprx, none of the fields have been hidden that are supposed to be hidden. Anyone have suggestions?
visible_fields = ['list', 'of', 'fields']
aprx = arcpy.mp.ArcGISProject(aprx_path)
map_doc = aprx.listMaps(map_frame_name)[0]
for lyr in map_doc.listLayers():
if arcpy.Describe(lyr).dataType == "FeatureLayer":
print(lyr.name)
# Get the layer's CIM definition - using Pro 2.9
cim_lyr = lyr.getDefinition('V2')
# Make changes to field properties
for fd in cim_lyr.featureTable.fieldDescriptions:
if fd.fieldName not in visible_fields:
fd.visible = False # Do not display this field
# Push the changes back to the layer object
lyr.setDefinition(cim_lyr)
aprx.save()