I have been racking my brain trying to figure out why I can't get this simple code to execute properly. I am trying to replace Datasources for numerous layers in numerous mxd's. I really don't want to do this manually! Essentially, a new file gdb has been created in a new folder with renamed feature classes. Feature class names were changed to simplify other geoprocessing scripts. Now I need to re-link the datasources to the new datasets.
I have a working code: it walks through a directory, it lists mxd's by file name, it finds layers and prints their names and finally saves the mxd. there are no bugs; however, when I open the mxd, the datasource has not changed. The script runs through the lyr.replaceDataSource but does not actually replace the datasource. I have used the validate {FALSE} parameter and still no luck! I am not a whiz when it comes to python, but I thought this was a pretty straight forward script.
Does anyone see what I am doing wrong? What am I overlooking?
Solved! Go to Solution.
Thanks for the suggestions, now that I know how to insert code I'll show what I've tested out.
first of all using the {False} parameter has shown some progress! the code works....sort of. So far it is changing the folder location, the FGDB name, and the feature class name but not the feature dataset name.
here is the test code:
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.supports("DATASOURCE"):
if lyr.dataSource == r"L:\Data\CFX_2016_160214\GDB\CFX_2016.gdb\CFX_IFS_Working_16\AA_Points":
lyr.replaceDataSource(r"L:\Data\CFX_2018_180214\gdb\CFX_2018.gdb\IFS_Working","FILEGDB_WORKSPACE","BB_Points", False)
lyr.name = "BB_Points"
print lyr.dataSource
and this is the output:
L:\Data\CFX_2018_180214\gdb\CFX_2018.gdb\CFX_IFS_Working_16\BB_Points
Is this something that the lyr.replaceDataSource() can't do?
if I use the {True} parameter, I don't get past line 4 and I think that is due to the fact that I have included the dataset name in the path. How do I navigate into feature datasets?
thanks again!
With version 10.5, I was having some luck using something like:
lyr.replaceDataSource(r"L:\Data\CFX_2018_180214\gdb\CFX_2018.gdb","FILEGDB_WORKSPACE","BB_Points", True)
# omitting 'IFS_Working' from the path
# L:\Data\CFX_2018_180214\gdb\CFX_2018.gdb\IFS_Working\BB_Points
It seems strange that the feature class name would be omitted from the path or layer name. This could cause problem if other feature classes in the file geodatabase contain an identical feature name. Could it be a bug?
Replacing the data source did not change the layer name, so that had to be set. Also, the old symbology was retained, so that may also need to be corrected.
Also, I was working inside ArcMap's Python window and would use arcpy.RefreshActiveView to update the map.
thank you,
I used your suggestions for removing the "IFS_Working" from the path, as well as adding "NONE" as the workspace (as it does not change).
Now the code automatically sources it to the correct dataset!
most of my trouble came from thinking too much (haha!), and mapping the path too far.
Thanks to everyone for the guidance I am but a greenhorn teaching myself...