Select to view content in your preferred language

Getting Alias Field names from layer in mxd 10.0 SP5 not working

4147
4
12-09-2013 02:00 PM
CameronBlandy
Regular Contributor
I am trying get the field aliases for each of the layers in an mxd document.  I have the following snippet that should do the trick.
import arcpy
from arcpy import env
import os, zipfile, zlib, shutil

mxd = arcpy.mapping.MapDocument(r"C:\\temp\OpenData\OpenData_test.mxd")
for lyr in arcpy.mapping.ListLayers(mxd):
  desc = arcpy.Describe(lyr)
  for field in desc.fields:
    print field.name, ":", field.aliasName, ":", field.type, ":", field.length, ":", field.domain
del mxd


When this code is run in stand-alone mode the describe gives an error (this is bug at 10.0 SPx apparently). When I run it through python in ArcMap using:
mxd = arcpy.mapping.MapDocument("CURRENT")
the program runs but the alias field names are not listed. I have spent a lot time trying to figure it out but based on my searches it seems that the issue lies with 10.0 SPx.

Can anybody confirm this or, alternatively, give some suggestions on ways I can get the alias field names for each layer in an mxd?
Tags (2)
0 Kudos
4 Replies
T__WayneWhitley
Honored Contributor
Have you tried the alternate ListFields access?

...as shown in this (untested) code excerpt from the 10.0 webhelp:
fields = arcpy.ListFields(lyr)
for field in fields:
    print("Field:       {0}".format(field.name))
    print("Alias:       {0}".format(field.aliasName))


And, I suppose, if there isn't an alias set, then nothing is returned (that is why I left in the print for field.name - if there's not a corresponding text print of the alias, then there isn't one).

Please post back if that works for you...


Wayne

EDIT:
Also, not sure if ListFields will take a map layer...I don't remember.  If not, get the layer's datasource path and use that with ListFields - it should at least work with a fc pathname.  Sorry, didn't check on that 1st.

Field
Resource Center » Professional Library » Geoprocessing » The ArcPy site package » Classes
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Field/000v00000071000000/

ListFields
Resource Center » Professional Library » Geoprocessing » The ArcPy site package » Functions » Listing data
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v0000001p000000
0 Kudos
T__WayneWhitley
Honored Contributor
I have 10.0 SP 5 installed on one of my machines and wanted to test that out - it works fine (see the Python 'live' coding within ArcMap below - I did not test stand-alone).  I added an alias, testAlias, to a dummy field, testfld, in a fc added to the map, a layer called 'testAlias' (sorry didn't mean to redundantly name it the same as my field alias - so don't be confused about that).
>>> mxd = arcpy.mapping.MapDocument('CURRENT')
>>> lyr = arcpy.mapping.ListLayers(mxd, 'testAlias')
>>> len(lyr)
1
>>> fields = arcpy.ListFields(lyr[0])
>>> len(fields)
5

>>> for field in fields:
... print('Field: {0}'.format(field.name))
... print('Alias: {0}\n'.format(field.aliasName))
...
Field: OBJECTID
Alias: OBJECTID

Field: SHAPE
Alias: SHAPE

Field: testfld
Alias: testAlias

Field: SHAPE_Length
Alias: SHAPE_Length

Field: SHAPE_Area
Alias: SHAPE_Area
>>>


So feeding in a layer obj from the map to ListFields works (ignore my warning in the last post).
Also, it appears you'll get an original field name as the field alias where no alias has been set.
0 Kudos
CameronBlandy
Regular Contributor
Thanks Wayne.

I did try what you suggested but still got the original names listed. Just to clarify the alias names are not stored in the database they are text changes made on the layer in the mxd (Properties->Fields).
My simplified code s:
mxd = arcpy.mapping.MapDocument('CURRENT')
lyr = arcpy.mapping.ListLayers(mxd)
for lyr in arcpy.mapping.ListLayers(mxd):
    if not lyr.isGroupLayer:
        print lyr.name
        fieldList = arcpy.ListFields(lyr)
        for field in fieldList:
            print(field.name + ": " + field.aliasName + ": " + field.type)
del mxd


So not sure what is happening here, but it is certainly frustrating.

Cameron

I have 10.0 SP 5 installed on one of my machines and wanted to test that out - it works fine (see the Python 'live' coding within ArcMap below - I did not test stand-alone).  I added an alias, testAlias, to a dummy field, testfld, in a fc added to the map, a layer called 'testAlias' (sorry didn't mean to redundantly name it the same as my field alias - so don't be confused about that).
>>> mxd = arcpy.mapping.MapDocument('CURRENT')
>>> lyr = arcpy.mapping.ListLayers(mxd, 'testAlias')
>>> len(lyr)
1
>>> fields = arcpy.ListFields(lyr[0])
>>> len(fields)
5

>>> for field in fields:
... print('Field: {0}'.format(field.name))
... print('Alias: {0}\n'.format(field.aliasName))
...
Field: OBJECTID
Alias: OBJECTID

Field: SHAPE
Alias: SHAPE

Field: testfld
Alias: testAlias

Field: SHAPE_Length
Alias: SHAPE_Length

Field: SHAPE_Area
Alias: SHAPE_Area
>>>


So feeding in a layer obj from the map to ListFields works (ignore my warning in the last post).
Also, it appears you'll get an original field name as the field alias where no alias has been set.
0 Kudos
T__WayneWhitley
Honored Contributor
ah, yes I see - apparently layer properties are not exposed in this way with arcpy, credit Get Spatial with this answer here:
http://gis.stackexchange.com/questions/27733/getting-the-field-alias-from-a-layer-from-within-a-map-...

I will have to check later to see if this has been added at 10.1 and later.  I thought it was interesting you can simply overwrite all layer properties with UpdateLayer but the way I understand it, this is not from using the fieldInfo obj - sorry if misleading there.  The alias stored in a layer in the map can be overwritten from settings already stored in a lyr file.  So guess you'd have to just know what they were (or perhaps read that from metadata?) since you do not apparently have read access at the layer object.


Wayne
0 Kudos