Being a recent graduate and just started working I'm fairly new with Python, but I'm trying my best to learn and I find it really fun and useful! However, I'm stuck with a problem I can't seem to solve on my own. We recently migrated our data from FGDB to an SDE database and I've got hundreds of lyr-files containing multiple layers which I need to change data sources for. The file names in the SDE, however, is not exactly the same as in the FGDB.

For example:

Name in FGDB: kl_c_Clip

Name in SDE: blabla.whatup.kl_c_y

As you can see part of the name is the same, which is pretty much always the case. I've been looking around for a solution but haven't found any where the names differ. I'm thinking the script should look something like this:

sdePath = r"Database Connections\Connection to thedatabase.sde"
mxd = arcpy.mapping.MapDocument("CURRENT")
for df in arcpy.mapping.ListDataFrames(mxd):
    for lyr in arcpy.mapping.ListLayers(mxd, ""):
        if lyr.isFeatureLayer == True:
            lyr.replaceDataSource(sdePath, "SDE_WORKSPACE", "")
del mxd

I run the script and it's loading, but nothing happens. No error or anything, and no change in data sources. I realize I have to get the name change in there somewhere, I just don't know where and how.

  • Your code sample doesn't include a save, so that might be th source of the "no change". You might need to two-step this process, by making an intermediate FGDB workspace with empty tables, which are renamed to that of the database suffix, or you might need to generate temporary layerfiles for each layer, create a new layer sourced to the database, and apply the layerfile symbology to the new layer. This is a "learn to swim by being catapulted into the ocean" task.
    – Vince
    Oct 16, 2019 at 10:33
  • Hi Vince, thanks for your quick comment! Yes, I'm starting to think I might be in over my head with this one. I added a print in the loop and it does iterate through all the layers (or sublayers rather, since it's a layer group) in the mxd. I have no idea where to go from there though. I'll take your suggestions into consideration and keep trying, thanks!
    – Kristoffer
    Oct 16, 2019 at 11:44


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.