Dynamic Text -- List Names of Layers in Table of Contents. Possible?

2653
1
02-11-2014 09:28 AM
AshleyHayes
New Contributor II
I currently have some text that lists the names of the layers found in the Table of Contents (TOC) of my map.  When I update this map with newer versions of some of the layers I then have to manually change the names of the layers in this text to the newer versions' names. 

For example, I might have a layer called Optic_Fiber_20140211 and in a few days I might receive a newer version called Optic_Fiber_20140215 ... I would then add the new version of the layer to my map, delete the old version of the layer from my map, and then manually edit the text to reflect that change.

It would be nice to have dynamic text that just lists the names of all of the layers in the TOC and then when I remove old layers and add new layers the text is automatically updated. 

Make sense?  Any suggestions?  Thanks!
0 Kudos
1 Reply
JeffBarrette
Esri Regular Contributor
This may work well for your specific scenario but there are many other scenarios where uses cases would NOT want the name to dynamically change. BUT ... this can be fully automated using arcpy.mapping.

There are several approaches.  The simplest my be finding the layer of interest in the TOC, changing its datasource to the new location and then simply changing its name property.  Here is an example:

mxd = arcpy.mapping.MapDocument(path)
lyr = arcpy.mapping.ListLayers(mxd, "some layer name")[0]
lyr.findAndReplaceWorkspacePath(path1, path2)
lyr.name = "some new name"
mxd.save()


Jeff
0 Kudos