This is a follow up problem to my previous dilemma: Turn Off Fields with Python which is working, however, we keep discovering things that need to be added to preserve all the properties of the original project (TOC order and visibility, group layers, disable popups, etc.). The final thing (we think) that needs to be preserved is field aliases. Now I know there are at least two ways to access this property - arcpy.ListFields() or arcpy.Describe().fields
Neither of which seem to work - they always list the actual field names, not the aliases, even when aliases exist.
I have been testing this directly in the Python window within Pro. Below is a screenshot of a test layer field table, with the two methods to access field properties below.
Is this another bug in Pro or is there something here that I completely missing..??
Solved! Go to Solution.
I too discovered that, and unfortunately, no the aliases have been stored in this specific Pro project, not with the data source. I have a clever yet cumbersome workaround, but hey, everything I had to do in the original script to turn the fields off was a clever and cumbersome workaround, what's another.
Save the layer file, hack into the JSON and get the field aliases like this: **shhh don't tell esri we are smart enough to do this**
aliases = []
with open(lyrx_path) as json_file:
data = json.load(json_file)
field_info = data['layerDefinitions'][0]['featureTable']['fieldDescriptions']
for f in field_info:
aliases.append(f['alias'])
Are the aliases associated with the layer or the data source?
Fields view: Current Layer drop-down menu
Edit field aliases on the data source
I just tested this and found that the modified field aliases were only printed if I changed the data source aliases. I think ListFields accesses the selected layer's source dataset, so changes to the layer's aliases aren't reflected in the result returned by the function.
I too discovered that, and unfortunately, no the aliases have been stored in this specific Pro project, not with the data source. I have a clever yet cumbersome workaround, but hey, everything I had to do in the original script to turn the fields off was a clever and cumbersome workaround, what's another.
Save the layer file, hack into the JSON and get the field aliases like this: **shhh don't tell esri we are smart enough to do this**
aliases = []
with open(lyrx_path) as json_file:
data = json.load(json_file)
field_info = data['layerDefinitions'][0]['featureTable']['fieldDescriptions']
for f in field_info:
aliases.append(f['alias'])
I think this the workaround I will have to use to do most of my Geoprocessing. So much of the functionality in Pro is just not available in arcpy but hacking into the LYRX may work and then using it to redefine my layers (numeric formatting etc)