This question already has an answer here:

We are in the middle of our ArcGIS 9.3 to 10.1 migration project and we have a lot of custom map documents (from different users) that among others, point to our current 9.3 SDE database. After our migration, we'll disable this database to the users so they can't connect anymore. That means they'll end up with broken links in their MXDs (that's basically what we want) and need to repoint to the new SDE.

I prepared a little script that should automatically repoint all broken layers to our new 10.1 SDE (using a connection file), but unfortunately I always run into the same error. I already checked out some other posts on this site (e.g. here and here ), but also on the ESRI forums e.g. here and the ESRI resource center of course. Also tried to repoint to e.g. a file geodatabase, but I'm getting the same error.

However, none of the suggestions apply to my case or worked for me, and after playing around almost the whole day now, I thought maybe someone here knows a solution to that problem. The code itself works fine, it finds the MXDs, spits out the broken layers if I ask it to, it's just the actual repointing command(s) that give me a headache..

Below my (simplified) code and the error I get. Thankful for any tips!

# Import system modules
import sys, string, os, arcpy, datetime, shutil

# source folder(s) to look for MXDs
sourcePath = r"D:\scripts\fixBrokenLinks\test"
# new 10.1 SDE connection
sde = r"\\path to *.sde connection file

# loop through folders and find all MXDs (put into a list)
for root, dirs, files in os.walk(sourcePath):
    for f in files:
        if f.endswith(".mxd"):
            mxdPath = root + "\\" + f
            mxd = arcpy.mapping.MapDocument(mxdPath)
            print "converting " + str(mxdPath)
            brknList = arcpy.mapping.ListBrokenDataSources(mxd)
            for brknItem in brknList:
##                print brknItem
                brknItem.replaceDataSource(sde, "SDE_WORKSPACE")#, brknItem, True)

##                # ALTERNATIVE:
##                find_workspace = brknItem.dataSource
##                replace_workspace_path = r"\\path to *.sde connection file
##                print "find_workspace " + str(find_workspace)
##                brknItem.findAndReplaceWorkspacePath(find_workspace, replace_workspace_path)

            newMXD = mxdPath[:-4] + "_NEW.mxd"
            del mxd

And here the error I get:

Traceback (most recent call last):
  File "D:\scripts\fixBrokenLinks\fixBrokenLinks_simple.py", line 19, in <module>
    brknItem.replaceDataSource(sde, "SDE_WORKSPACE")#, brknItem, True)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\_mapping.py", line 680, in replaceDataSource
    return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True)))
ValueError: Layer: Unexpected error

I'm using ArcGIS 10.1 SP1 (Build 3143) and Python 2.7.2

marked as duplicate by blah238, BradHards, PolyGeo, Paul, Fezter Nov 14 '13 at 6:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Thanks for pointing into the right direction, the solution provided in gis.stackexchange.com/questions/32468/…"> does work. Just wondering though why I did not come across this one, I probably have to refine my search queries. – David Nov 14 '13 at 22:50
  • If you're bulk updating MXDs with lots of layers to replace a data source to an SDE source you can run into the 'unexpected error' because of something going on with the database connection. You can reset it by adding the line 'arcpy.ClearWorkspaceCache_management()" after the "del mxd" line – Marc Pfister Feb 3 '17 at 20:57