How to Set Data Source of a Layer In an MXD From SDE connection

1481
5
08-28-2017 09:40 PM
BehrouzHosseini
Occasional Contributor

Can you please take a look at this example and let me know how to I can replace Data Source data of a layer in a saved mxd map (Fuse) to a layer called SDEElectric.DBO.Fuse which is in an enterprise geodatabase through database connection

I tried this code but it didnt work

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\GIS\Maps\Electric.mxd")
src =r"C:\Users\xxx\AppData\Roaming\ESRI\Desktop10.4\ArcCatalog\SDEElectric.sde\SDEElectric.DBO.Dataset\SDEElectric.DBO.Fuse"
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
 if lyr.name == "Fuse":
 lyr.replaceDataSource(src, "FILEGDB_WORKSPACE", "")
mxd.save()
del mxd
print "done"‍‍‍‍‍‍‍‍‍‍
0 Kudos
5 Replies
Asrujit_SenGupta
MVP Regular Contributor

Not a Python expert, but 

lyr.replaceDataSource(src, "FILEGDB_WORKSPACE", "")

Shouldn't the above reflect an Enterprise gdb, like below:

lyr.replaceDataSource(src, "SDE_WORKSPACE", "")

If the above doesn't help, share the errors you receive and that may also help someone to provide an answer....

0 Kudos
BehrouzHosseini
Occasional Contributor

Thanks Asrujit SenGupta

but still getting same error

0 Kudos
Asrujit_SenGupta
MVP Regular Contributor

What errors do you receive?

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

you may want to look at /blogs/myAlaskaGIS/2015/08/31/python-addin-for-data-inventory-and-broken-link-repair?sr=search&searc...‌ to see samples of how I view and replace sources in mxd.  It's based mainly on broken-links, so may not work for you, but just in case.

0 Kudos
BehrouzHosseini
Occasional Contributor

Thanks Rebecca, I already spend some time there but it was not really helpful as it mainly focus was on fixing broken links

0 Kudos