Setting Field Visibility

3677
6
Jump to solution
04-24-2014 09:18 AM
JohnDye
Occasional Contributor III
Anyone know how to make fields in a Feature Layer visible or hidden?

Tried using the Field Info object, no luck there...
desc = arcpy.Describe("FeatureLayer") field_info = desc.fieldInfo visibleFields = ["ID", "NAME", "Rank", "Count"] for index in xrange(0, field_info.count):     fieldName = str(field_info.getfieldname(index))     if fieldName in visibleFields:         print "Found Field " + str(fieldName) + " in visibleFields. Setting Visible..."         field_info.setvisible(index, "VISIBLE")     else:         print "Field " + str(fieldName) + " not found in visibleFields. Setting Hidden..."         field_info.setvisible(index, "HIDDEN")


Tried tossing a RefreshActiveView() onto the end, that didn't help either.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MattEiben
Occasional Contributor
You're definitely on the right track there.  I ran into this problem a couple months ago.  The key is that field info is a read only attribute, you can't write setvisibility on field in an existing feature layer.

The general workflow is like you have it above.  Make your field info object base on your existing feature layer, then you want to make a new feature layer using the optional "field_info" attribute.

Make Feature Layer (Data Management)

Here my code for that.  I took your code and simplified it just a little.

visibleFields = ["ID", "NAME", "Rank", "Count"] field_info = arcpy.Describe("FeatureLayer").fieldInfo for index in xrange(0, field_info.count):     if field_info.getfieldname(index) not in visibleFields:         field_info.setvisible(index,"HIDDEN")  arcpy.MakeFeatureLayer_management("FeatureLayer","Feature_Layer_With_New_Visibilities","","",field_info)


If you have symbology and/or labeling, this can mess with that too.  If you're having issues with that, I can dig up a code example.

Hope this helps!

View solution in original post

6 Replies
JoshuaChisholm
Occasional Contributor III
Hello John,

Cool script. I haven't seen that before.

I'm not sure if this will help, but the help documentation uses "setVisible" (not "setvisible"). Same for "getFieldName".

Good luck!
~Josh
0 Kudos
JohnDye
Occasional Contributor III
Hey Josh,
That's true, I didn't notice that. Interesting thing is that the intellisense in the python window suggests setvisible, not setVisible.

I changed it anyway, but still no dice.
It doesn't throw an exception in either case which is also strange.

It would be really cool if it worked. lol. Would be nice to be able to turn fields off prior to an export in order to omit them from the result.
0 Kudos
MattEiben
Occasional Contributor
You're definitely on the right track there.  I ran into this problem a couple months ago.  The key is that field info is a read only attribute, you can't write setvisibility on field in an existing feature layer.

The general workflow is like you have it above.  Make your field info object base on your existing feature layer, then you want to make a new feature layer using the optional "field_info" attribute.

Make Feature Layer (Data Management)

Here my code for that.  I took your code and simplified it just a little.

visibleFields = ["ID", "NAME", "Rank", "Count"] field_info = arcpy.Describe("FeatureLayer").fieldInfo for index in xrange(0, field_info.count):     if field_info.getfieldname(index) not in visibleFields:         field_info.setvisible(index,"HIDDEN")  arcpy.MakeFeatureLayer_management("FeatureLayer","Feature_Layer_With_New_Visibilities","","",field_info)


If you have symbology and/or labeling, this can mess with that too.  If you're having issues with that, I can dig up a code example.

Hope this helps!
MattEiben
Occasional Contributor
Thanks Wayne_Whitley!

I was just about to reference my own post and you beat me to it.
0 Kudos
JohnDye
Occasional Contributor III

The general workflow is like you have it above.  Make your field info object base on your existing feature layer, then you want to make a new feature layer using the optional "field_info" attribute.


Thanks Matt & Wayne, I got it going now. Good to know that an export is required.

Geeze ESRI, I don't even have words for this nonsense. This is so not neccesary...
0 Kudos