2

I am trying to repoint layer (.lyr) files to a new SDE connection. In a test project folder I have two test layer files.

When using lyr.findAndReplaceWorkspacePath() the script runs with no errors, but only the first layer in the directory gets properly updated to point to the new SDE path.

When using lyr.replaceDataSource()the script produces an error while attempting to repoint the second layer to the new SDE path:

ValueError: Layer: Unexpected error 

(Error is referring to the line containing lyr.replaceDataSource())

So something is running afoul on the second iteration of the for loop.

I have tested multiple .lyr files, and it's nothing wrong with any of the actual .lyr files. It's just whichever .lyr file is up second. So whichever is first, is properly repointed to SDE, and whichever is second, is not.

import arcpy, os

layersFolder = r"I:\FolderX\Kristen\FolderY\Repoint_lyrs\TestLayers\MultipleLayers"
newSDEpath = r"I:\FolderX\Kristen\FolderY\Repoint_lyrs\gis_sde.sde"

for dirname, dirnames, filenames in os.walk(layersFolder):

    for filename in filenames:
        path = os.path.join(dirname, filename)
        basename, extension = os.path.splitext(path)

        if extension == ".lyr":
            lyr = arcpy.mapping.Layer(path)

            if lyr.isFeatureLayer:

                if lyr.supports("DATASOURCE"):

                    lyrName = lyr.datasetName
                    print lyrName

                    print '\t' + "newSDEpath: " + newSDEpath
                    print '\t' + "lyr.datasetName: " + lyr.datasetName

                    lyr.replaceDataSource(newSDEpath, "SDE_WORKSPACE", lyr.datasetName, True)

                    #lyr.findAndReplaceWorkspacePath(lyr.dataSource, newSDEpath, True)

                    lyr.save()

                    print '\t' + "Changed path: " + lyr.dataSource + '\n'

                    del lyr
  • I've seen a few weird issues when re-pathing data using the replaceDataSource and finaAndReplaceWorkspacePath functions. Don't know why but sometimes the non-static parameters screw things up in iteration. Since you already created the lyrName object, use this instead of lyr.datasetName in later calls. This, in theory shouldn't do anything different, but like I said, the non-static parameters are funky sometimes. – GeoJohn Feb 11 '16 at 19:39
  • Also, you could throw a "try" and "except" in there somewhere to see if it goes beyond the second file it finds. Might help diagnose the problem. Overall, your code looks good. I don't see anything that really stands out. – GeoJohn Feb 11 '16 at 19:41
  • Sorry, one more thought just crossed my mind... If any of your layers are group layers, this presents an entirely new recursion problem that needs to be solved before you can re-path anything. – GeoJohn Feb 11 '16 at 19:43
  • @GeoJohn - Exactly. When I test with static parameters it works fine. It doesn't like the iteration. Also, I tried using lyrName in the function, and correct that doesn't help either, although maybe it's a 'better' best practice. I will change it. As for the "try"/"except" if there are more than two it doesn't work either... Also, there are no group layers in this test. My longer script does include code for that, though. Hmm... – Kristen G. Feb 11 '16 at 22:21
  • You ever get this working? – GeoJohn Feb 15 '16 at 15:02
1

Here is a script tool version. I use this all the time on both layers and MXDs. Shouldn't make a difference whether or not you are in an SDE or otherwise.

import arcpy, os

folderPath = arcpy.GetParameterAsText(0)
findString = arcpy.GetParameterAsText(1)
replaceString = arcpy.GetParameterAsText(2)

for root, dirs, files in os.walk(folderPath):
    for f in files:
        if f.endswith(".mxd"):
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Path: " + root + "\\" + f)
            currentMXDfile = root + "\\" + f
            mxd = arcpy.mapping.MapDocument(currentMXDfile)
            arcpy.AddMessage(" ")
            arcpy.AddMessage(currentMXDfile)
            arcpy.AddMessage(" ")

            try:
                mxd.findAndReplaceWorkspacePaths(findString, replaceString)        
                mxd.save()
                arcpy.AddMessage("MXD data sources have been updated!" + root + "\\" + f)

            except:
                arcpy.AddWarning("Unable to update: " + root + "\\" + f)

        elif f.endswith(".lyr"):
            arcpy.AddMessage(" ")
            arcpy.AddMessage("Path: " + root + "\\" + f)
            currentLYRfile = root + "\\" + f
            lyr = arcpy.mapping.Layer(currentLYRfile)
            arcpy.AddMessage(" ")
            arcpy.AddMessage(currentLYRfile)
            arcpy.AddMessage(" ")

            try:
                lyr.findAndReplaceWorkspacePaths(findString, replaceString)        
                lyr.save()
                arcpy.AddMessage("Layer data sources have been updated" + root + "\\" + f)

            except:
                arcpy.AddWarning("Unable to update: " + root + "\\" + f)

        else:
            arcpy.AddMessage("File is not a .mxd or .lyr and will be ignored.")
  • I'll take a look at this tomorrow when I'm back at work and let you know how if I can get anything out of it. Thanks a lot! – Kristen G. Feb 11 '16 at 22:22
  • OK, so I took a look at this just now and adjusted some of my code to match yours. It goes through the script without errors, but doesn't repoint. When I change the repoint function from findAndReplaceWorkspacePath() to replaceDataSource() it updates the first layer, but again, whatever layer shows up second, does not repoint and throws that "ValueError: Layer: Unexpected Error" error. I'm going to call ESRI. Pending they can get things working for me, I'll post the new result as an answer. – Kristen G. Feb 16 '16 at 14:32
  • @Kristen G. Yes, please do post what they say. I'm curious. – GeoJohn Feb 17 '16 at 15:28
  • ESRI was very unhelpful. The guy I spoke with said he hasn't written Python in over a year and a half and kept sending me scripts to try that had nothing to do with my problem. Meh. I just sent it out to my entire group to see if anyone has any other ideas. Just wanted to give you an update. I promise to post an answer if there ever is one! – Kristen G. Mar 15 '16 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.