Skip to content

Instantly share code, notes, and snippets.

@mu88
Created March 1, 2018 16:13
Show Gist options
  • Save mu88/c4f87f87e809acfd63c7721474bd4111 to your computer and use it in GitHub Desktop.
Save mu88/c4f87f87e809acfd63c7721474bd4111 to your computer and use it in GitHub Desktop.
ArcPy - Find duplicate Field Aliases within MXD
import arcpy, os
os.system('cls')
mxd = arcpy.mapping.MapDocument(r"\\Server\Share\MyMap.mxd")
tolerateFieldWhenDisabled = True
for layer in arcpy.mapping.ListLayers(mxd):
print "Checking Layer '" + layer.name + "'"
if not layer.isFeatureLayer:
print "Layer '" + layer.name + "' is no feature layer"
continue
layerDescription = arcpy.Describe(layer)
fieldInfo = layerDescription.fieldInfo
distinctAliasNames = []
# unfortunately, this script does not work
# when using ListFields(), the initially used SDE connection has to be present to
# retrieve the feature class.
# when using layerDescription, no aliases will be returned
#
# see here for more http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/layer-class.htm
# => field alias of MXD is not supported, so the SDE file HAS TO EXIST
fields = arcpy.ListFields(layer.dataSource)
##fields = layerDescription.fields
for field in fields:
print field
aliasName = field.aliasName
if aliasName not in distinctAliasNames:
distinctAliasNames.append(aliasName)
else:
if tolerateFieldWhenDisabled == True:
fieldIndex = fieldInfo.findFieldByName(aliasName)
visibility = fieldInfo.getVisible(fieldIndex)
if visibility == "VISIBLE":
raise Exception("Layer '" + layer.name + "' contains duplicate field alias '" + aliasName + "'")
else:
raise Exception("Layer '" + layer.name + "' contains duplicate field alias '" + aliasName + "'")
print "Completed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment