First of all, this is not a duplicate thread as I am not convinced that the one below has been resolved..at least the suggestions and code does not seem to work for me.
Turning fields on and off using arcpy
Another on stackexchange on this topic addresses this in ArcMap, but since ESRI decided to get rid of the mapping module and create the mp module for Pro, it doesn't solve the problem either.
https://gis.stackexchange.com/questions/230017/using-fieldinfo-setvisible-in-arcpy
We have a Pro project that houses our web maps for our organization. Recently we turned on editor tracking fields for layers coming from a specific sde, and now we need to hide those fields so they don't display in the web map pop-ups. To get an idea of the scale of this task, there are over a thousand layers that need these fields turned off. I have been scripting in Python for ArcMap for several years, but am relatively new to Pro and Python 3 and the new arcpy.mp module. The example code in the above threads uses arcpy.mapping and looks fairly straightforward - the arcpy.mapping.UpdateLayer() function does the hard work. Well this does not exist in arcpy.mp and the workaround I have seen (and suggested by esristaff) goes like this:
- create a fieldinfo describe object on the layer of interest
- loop through fieldinfo list and using setVisible hide desired fields
- make feature layer and pass in fieldinfo
- save as layer file
- replace old layer in map with new layer (plus a bunch of steps to preserve order and symbology)
This is all straightforward to me and I am not having issues structuring the code. The issue is that the temp layer created from MakeFeatureLayer is not honoring the new field info. I have tested this interactively in the Python window in Pro:
fieldInfo = arcpy.Describe(lyr).fieldInfo
Printing this shows the fields I need to turn off as visible. Then looping through and turning them off:
for i in range(0, field_info.count):
if field_info.getFieldName(i) in fields_to_turn_off:
field_info.setVisible(i, "HIDDEN")
Printing field_info now shows the fields I wanted off to be hidden. Great. But then I make a feature layer passing this field_info into it, it shows up in my TOC, I open the fields window on it and all the fields are still on!
The example code from the previous threads stops there, but to me it seems like either the field_info object is not being 'saved' or the MakeFeatureLayer function is not honoring it as a parameter. ESRI's documentation on fieldInfo and everything else is minimal at best, and I have not come up with anything else helpful on Google.
Here is an overly complicated workaround I have gotten to work.
- Save layer of interest as a layer file
- Then on that layer file create the describe object and turn fields off
- Then make feature layer passing in the new field info
- Then save as another layer file
Kind of ridiculous.