rangermry

Replace Data Source

Discussion created by rangermry on Mar 14, 2013
Latest reply on Mar 15, 2013 by rangermry
I try this code and then my path changes to the path of the mxd plus the sde connection.  I am not sure I am going in the right direction anyways?  I am trying to change all my SDE files that point to one server to another with the same feature datasets and names.  Please Help.


#Set Your Variables Up Here!
#   Must add prefix r (stands for raw) and put path in quotes. 
targetMXD = r"\\filepathhere.mxd"
userName = "me"

#   Set variables for creating new SDE Connection
folderName = "C:/Documents and Settings/" + userName + "/Application Data/ESRI/Desktop10.0/ArcCatalog"
fileName = "Connection to NewSDETest.sde"
serverName = "SP56"
serviceName = "5151"
databaseName = "gis"
authType = "DATABASE_AUTH"
username = "look"
password = "look"
saveUserInfo = "SAVE_USERNAME"
versionName = "SDE.DEFAULT"
saveVersionInfo = "SAVE_VERSION"
#Process: Use the CreateArcSDEConnectionFile function
mxd = arcpy.mapping.MapDocument(targetMXD)
for df in arcpy.mapping.ListDataFrames(mxd):
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.supports("SERVICEPROPERTIES"):
            servProp=lyr.serviceProperties
            sdeFile= '"'+folderName+'/'+fileName+'"'
            if (lyr.serviceProperties["ServiceType"]=="SDE") and (lyr.serviceProperties["Service"]== "5153"):
                dataSet=lyr.dataSource
                list=dataSet.split('\\')
                newDataSet=str(list[2])
                currentName=newDataSet.split('.')[0]
                newDataSet.replace(currentName,userName)
                lyr.replaceDataSource(sdeFile,"SDE_Workspace",newDataSet,"")
                print lyr.dataSource
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
mxd.save()
del mxd

Outcomes