I've been trying to turn all fields of a layer through properties fields

I just need to turn them all on

 import arcpy
 from arcpy import env
 import sys
 import os
 mxd = arcpy.mapping.MapDocument("CURRENT")
 df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
 for lyr in df:
     if lyr.name == "roads":
         lyr.visible = True
##Then here I want to turn all fields on of the layer name roads
  • I think that you would need to start a new loop, something like this: field_info = desc.fieldInfo for index in xrange(0, field_info.count): fieldInfo.setVisible( index, 'VISIBLE' ) – enolan Oct 2 '18 at 15:30
lyr = r"path to your dataset"
fldlist = arcpy.ListFields(lyr)
fieldinfo = ""
doc = arcpy.mapping.MapDocument("CURRENT") # Current mxd
df = arcpy.mapping.ListDataFrames(doc)[0] # First dataframe
for fld in fldlist:
    fieldinfo = fieldinfo + fld.name + " " + fld.name + " VISIBLE;"
nlyr = arcpy.MakeFeatureLayer_management(lyr,"new layer name","#","#",fieldinfo) # Create the feature layer
arcpy.mapping.AddLayer(df,nlyr) # Add new layer to current mxd

To hide the fields, change "VISIBLE" to "HIDDEN".

You'll need to get the field list, set the fieldInfo, then apply it in makefeaturelayer. You cannot change the fieldInfo properties of a layer that is already in the mxd, so you have to create a new layer with the fieldInfo set to visible.

I am adding this answer because this question hasn't been answered here before. Generally, you should show some more effort in your question, not just "here's my code, please finish it for me." Tell us what you've tried, where you're stuck.

| improve this answer | |
  • You're right I should put more context to my question. You mean it is impossible to Turn All Fields On command from the Table Options menu in a layer without creating a new layer!? – Max Bridge Oct 2 '18 at 17:23
  • It's not possible through arcpy. it's been added as a feature request to esri, but they haven't added the functionality yet. – jbalk Oct 2 '18 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.