Trying to update data source for layers in ArcGIS Pro maps programmatically. So, I must be misunderstanding the syntax for doing that explained here. Because the following does not work:
import arcpy
aprx = arcpy.mp.ArcGISProject(r'path to my project file')
#This gets me list of all the maps
maplist = aprx.listMaps()
#If I only want those belonging to MyGroup
mygroup = aprx.listMaps('MyGroup')[0]
#To get the layers in that group
grouplayers = mygroup.listLayers()
new_sdeConn = r'path to my SDE connection file'
for lyr in grouplayers:
old = lyr.connectionProperties #also tried lyr.connectionProperties['connection_info']
lyr.updateConnectionProperties(old, new_sdeConn)
This doesn't throw an error. it just doesn't do update the data sources as desired. The ESRI example goes like:
aprx.updateConnectionProperties(r'C:\Projects\YosemiteNP\Vector_Data\Yosemite.gdb',
r'C:\Projects\YosemiteNP\DBConnections\Server.sde')
Does this maybe only work for the project as a whole?
I have just concluded the same for 2.7.
Updating and fixing data sources funtion does not update the datasources nor the instructions in Update the data source between Enterprise geodatabases in ArcGIS Pro using ArcPy solve the problem. Connections don't update.
Funny. My mentor has just asked me to test it in 2.7 for ArcMap... Interested to see if it'll work for us. I'll keep this thread updated on my progress.
Works in 2.7 for ArcMap using:
lyr.findAndReplaceWorkspacePath(old_path, new_path, True)
Don't fix what's already working, Esri...
I found that if the layer have Join to some table then the syntax of the connectionProperties change and you cannot just replace it.
You must remove the join and then you can update.
Then you can renew the join if the table exists
Old thread, but seems to get a lot of traffic and thought I'd add a solution that worked for me. Needing to update paths from shapefiles to a GDB, I think that the ESRI documenation for replacing parts of the path string is assuming that it is still a GDB to GDB update, like all of their other examples. Their provided example syntax failed to update the paths so after some digging I came up with creating the connection property, which worked:
lyrcon = {'dataset': lyr.name, 'workspace_factory': 'File Geodatabase', 'connection_info': {'database': newDataPath}}
lyr.updateConnectionProperties('', lyrcon)
I went for the full-on sledge-hammer approach. Here's what worked for me:
yay! it works at last!
Could you please share your implementation? It does sound like a good, hacky approach but it'd be nice to see it with some actual code.
Thanks.
The code snippet below is part of my Python tool which makes this easy.
In my code, this is run within a loop for multiple layers and it very slow, as it has to wait for ArcGIS Pro to add each layer to the map and then remove it again. ArcGIS Pro is the most unresponsive application I've used on any OS anywhere anytime ever (can you tell it annoys me? A lot.).
Before this snippet, the logic gets a list of all featutre classes in the new Data Source and for each of the old feature classes to be changed, it checks the list to make sure that there is one with a matching name in the new Data Source to use as 'fcNew'. If there is not, then my particular tool will output a warning, and depending on another parameter in the tool, may also remove the layer if its data source could not be updated (due to no matching FC in the new DS).
fcNew = os.path.join(newDS, fcName)
tmpLayer = map.addDataFromPath(fcNew)
newProperties = tmpLayer.connectionProperties
map.removeLayer(tmpLayer)
## arcpy.AddMessage("OLD connection properties:\n{}".format(layer.connectionProperties))
layer.updateConnectionProperties(layer.connectionProperties, newProperties)
## arcpy.AddMessage("NEW connection properties:\n{}".format(layer.connectionProperties))
I'm just responding here to be updated if this is ever resolved. Perhaps in another 4 years. My code and problem were pretty identical to original post - but moving from one gdb to another gdb with Feature Classes all of the same, exact name. Tried all the solutions offered here in the comments, to no avail.
Honestly not a bad, temporary solution if your situation allows is as follows: 1) Disconnected drive or server containing path needing change 2) Open project 3) Click the red exclamation point from all the broken paths and fix the broken paths. In my case, much of the original data was on a gdb on an external hard drive. If within a Pro project on server 1a) copy it to external hard drive 2a) disconnect from server 3a) fix paths as above 4a) copy back to server / disconnect external hard drive 5a) fix paths again, setting them to desired new gdb. Screw it.
HOLY COW TI ! Your solution, although a roundabout one, worked pretty slick! I can't believe it's been so hard to find a SIMPLE solution.. Cheers.