Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Replace Data Source

$
0
0
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.

Code:


#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


Viewing all articles
Browse latest Browse all 2485

Trending Articles