yguan13

Using arcpy.mapping, how to rename sde feature class in mxd files

Discussion created by yguan13 on May 1, 2013
Latest reply on Jul 24, 2013 by mdenil
We have many mxd files by different users and I want to change one SDE feature class name in all those mxd files without changing SDE connections. As a test, I thought this script would work, but I always get "Unable to change name" exception. Where am I wrong?

mxd = arcpy.mapping.MapDocument(r"L:\users\guana\test_fc_rename.mxd")
for df in arcpy.mapping.ListDataFrames(mxd):
    for lyr in arcpy.mapping.ListLayers(mxd,"",df):
        if lyr.supports("DATASOURCE"):
            if lyr.dataSource.find("SDE_GOM.LEXCO_LEASE") <> -1:
                (str1,str2,fname) = lyr.dataSource.split("\\")
                sdecon = str1 + "\\" + str2
                print "sdecon = " + sdecon
                try:
                    lyr.replaceDataSource(sdecon,"SDE_WORKSPACE","SDE_GOM.LEASE_LEXCO")
                except:
                    print "Unable to change name"

Outcomes