If you have been using arcpy for a while, you might have found that there are certain limitations. For instance, you cannot:
- create a new map document;
- create/modify map document layout elements (for instance, change scale bar units or labels);
- create map grid /graticules in layout;
- edit network dataset properties;
Another fairly basic thing that you cannot do is to view/edit field aliases for layers in a map document. This is a quite common scenario when you have authored a map document using a feature class from a geodatabase. By default, the field aliases for the map layers you create are generated using field aliases of the feature class fields stored within the geodatabase. However, as soon as you’ve created a map layer from a feature class, the layer fields have no connection to their data source fields aliases.
This means that if you would change the alias of a feature class field, this won’t be reflected in the layer properties that refer to the feature class. Of course, you can always manually update the field aliases using Layer Properties window in ArcMap, however this can get complicated if you are not sure what fields should be updated.
This is because it is currently (ArcGIS 10.4.1) impossible to list the field aliases for a map layer. When you are using arcpy.ListFields() function, you won’t be able to get aliases for the fields that you can see from the Layer Properties window. This is because this function will pull the field aliases from the layer’s data source, that is a feature class.
This implies that if you have a bunch of map documents and you would want to update the map layers field aliases for them in bulk, you cannot use arcpy. You have two options really:
- Use X-Ray for ArcMap 10.1 add-in. Then you can open every map document file you want to apply changes to and use the command on the add-in’s toolbar.
- Use ArcObjects to access the map document file programmatically, make required changes in the layer field properties and save it.
I think it would be great if we could make those changes using arcpy. A common use case is when you have a suite of ArcGIS Server map services you maintain and in a case when a geodatabase feature class field alias is changed, you would like to reflect it in the service as well. So, you would need to update the field aliases for the map documents and then republish them. Obviously, you would like to script this.
Unless you would like to build a custom tool in .NET/Java, you would call comtypes package from Python to invoke ArcObjects COM methods. I have blogged about using comtypes for accessing ArcObjects from Python before, so be sure to check this post.
Here is a snippet of code I use for automating field aliases updates. I hope this can save you some time.