Select to view content in your preferred language

Ability to Turn Off Fields on an existing Layer with Python in ArcGISPro

1451
3
03-28-2021 07:11 PM
Status: Already Offered
LeandraGordon
Frequent Contributor

This is related to the post: https://community.esri.com/t5/python-questions/turn-off-fields-with-python/td-p/403316/page/2
by @DylanHarwell 


There is a need to be able to turn on and off Fields in Python on Layers that have been created by joins or on existing layers in a project via ArcPy. 

This is a sample workflow:
1. Create a new feature class via Spatial Join of 2 Layers

2. Use Create Feature Layer to create a layer from the Feature Class

3. Create a creates a geoprocessing field info object from the new Layer.

4. Iterate through the field info object to set fields as hidden or visible

5. NEW: Use the Field Info object to set the fields in the new Layer created earlier.

OR make the arcpy.Describe().FieldInfo function able to access a new Feature Class that not currently displayed as a Layer.There were (cumbersome) solutions for ArcMap.

3 Comments
JeffBarrette

Leandra,

This can be done using Python CIM Access.  Here is the help topic that describes what that means and there is even a sample that does exactly what you are requesting.

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/python-cim-access.htm

Code snippet under Example 2: Modify field properties

 

# Reference a project, map, and layer using arcpy.mp
p = arcpy.mp.ArcGISProject('current') 
m = p.listMaps('Map')[0]
lyr = m.listLayers('GreatLakes')[0]

# Get the layer's CIM definition
cim_lyr = lyr.getDefinition('V2')

# Make changes to field properties
for fd in cim_lyr.featureTable.fieldDescriptions:
    if fd.fieldName == "OBJECTID":
        fd.visible = False            #Do not display this field
    if fd.fieldName == "Shape_Area":
        fd.alias = "Area (hectares)"  #Change field alias

# Push the changes back to the layer object
lyr.setDefinition(cim_lyr)

 

I hope this helps.

Jeff - Layout and arcpy.mp teams

 

KoryKramer
Status changed to: Already Offered
 
LeandraGordon

Thanks Again!