Updating Data Source in ArcGIS PRO project with arcpy

13291
34
03-22-2019 10:55 AM
Arne_Gelfert
Occasional Contributor III

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?

0 Kudos
34 Replies
RonnieRichards
Occasional Contributor III

This process is inconsistent for us switching from one SQL SDE source to another. It's a complete mess and used to work just fine in ArcMap/arcpy.

For us if the server name is the only change then the find / replace dictionary works but if anything else is different (database, user, instance) than the find/replace dictionary does not work. 

The old / new .sde connection approach has never worked for us 

There is a bug in ArcGIS Pro 3.0.3+ if you try to update the WORKSPACE on a map manually it breaks the layers in map document which could be one reason it might not be working consistently with Arcpy. 

https://my.esri.com/#/support/bugs/bugs?bugNumber=BUG-000156145 

Not sure why they are reporting "as designed" as I would want the workspace option to work like it did in ArcMap. Guess we are asking too much!! 

We also created an enhancement because ArcGIS Pro references to the SDE connection properties differently depending on where you look. If they cannot get these properties to be consistent how could any repathing possibly work?

https://my.esri.com/#/support/bugs/bugs?bugNumber=ENH-000161406

0 Kudos
StaticK
MVP

I found that doing an SDE to SDE change worked for us only if we provide the full path to the new connection file to the connection_info['database'] property- trying to do the dictionary change method or just the 'find replace' name of the connection file bombs.

It would be nice if we could get a success/ fail result from the process instead of it just moving on giving a false positive that it worked. We could print the connection again, but that is ugly.

0 Kudos
RonnieRichards
Occasional Contributor III

Interesting. I would be curious what you provide in the current SDE connection input since Pro Maps do not appear to maintain reference to the original .sde connection file. Do you obtain this from Describe or always know the current connection? 

0 Kudos
StaticK
MVP

I dynamically created the full path to the previous sde file in other code, which I use for the input.  Maybe it could work with using just the name of the sde file, but since I built out the path using the layer connection_properties, some lyr name mapping, the layer dataset property, and some regex to generate the old path and path to the new connection file and it worked as I needed, I kept it.

0 Kudos
RonnieRichards
Occasional Contributor III

Thank you for this description. I have been attempting to replace the .sde connections by using the workspace which comes back in describe method. This always appears to be a temporary .sde connection in the user profile for some strange reason. When creating the full path using the temporary .sde workspaces as the current input it never seems to update the data sources.

However your tip led me to the conclusion is we know where already know where all the original sde connection files are used to create the initial map. Using this as the full path+.sde connection file in the current input seems to be working for all of our use cases!

For us this includes different server names, named SQL instances, database name differences, username/pwd. This is even working on query layers from the non-geodatabase SQL databases. In each of these use cases something was challenging when using the find/replace dictionaries in updateConnectionProperties. It worked for some but not others such as the database name differences.

Feel confident we can now proceed with the .sde replacement option! Thanks

 

 

0 Kudos