Select to view content in your preferred language

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

2460
4
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.

4 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!

jmk_307

I'd argue the method suggested as the fix has too many restrictions that render the "already offered" insufficient and inadequate. 

The AlterField method does not work and instead throws an error:
ERROR 000499: table is not editable

Is this AlterField hack intended for unversioned datasets only?

This must be ran on an open Pro aprx, as CURRENT? That is not helpful for turning it into a reoccurring maintenance script/ process.

From the docs: "Chances are that if you add a new feature class or table to a map, the CIM will not have any fieldDescriptions and therefore, you won't be able to modify the field properties."  When does it not become 'new'?  Closing and reopening the project doesn't seem to change anything- for scripting it makes logical sense that we apply field visibilities/ definition queries to these datasets in one script operation.

We are most likely trying to programmatically create/ set views and hide certain fields when we are working in the CIM and even running a tool that modifies tables is not a good idea as a solution, even if it says it doesn't alter it.

Why are the fields fieldDescriptions not populated when the layer is first loaded?

cim-fielddescriptions-is-not-accessible-for-some