AnsweredAssumed Answered

ArcPy Layer updateConnectionProperties not working as expected

Question asked by dbened@denverwater.org_DW on May 16, 2019
Latest reply on Aug 3, 2020 by dbened@denverwater.org_DW

I am attempting to use Arcpy Layer updateConnectionProperties method to change the database connection for each layer in an ArcPro project from my SDEDEV environment to an SDEUAT environment. Note that both database are identical copies so they only vary in the database name/servername, instance name and PWD. In our organization this is a common task when we are publishing services in a DEV environment. Once we are ready for Users to Test (UAT) our apps and underlying services/maps we publish to UAT. This workflow allows us the ability to continue developing new stuff without affecting the users that are testing the stuff that was developed in previous deployments. When we used ArcMap we had a python toolbox tool that would re-map the database connections for each layer in a map from DEV to UAT and Finally to PROD once we were ready for the final deployments. Our goal is to have a similar workflow, I would like to do use some ESRI ArcPy Pro Python code to update the connection information. I assumed that the "updateConnectionProperties" method should be able to do this.

 

After researching and comparing other solutions and using ESRI documentation here I was able to come up with the following code:

 

import arcpy
aprx = arcpy.mp.ArcGISProject(r'C:\ProProjects\MyProj\TheProPoject.aprx')
for theMap in aprx.listMaps():
print("%-24s %s" % ("Map Name:", theMap.name))
for lyr in theMap.listLayers():
if lyr.supports("CONNECTIONPROPERTIES"):
print("%-24s %s" % ("layer Name:", lyr.name))
lyr.updateConnectionProperties(lyr.connectionProperties,r'C:\ProProjects\MyProj\DatabaseConnections\ARCGISSERVER@SDEUAT2.sde', True, False)
aprx.save()
del aprx
print("done")

 

This solutions kinda works but with some caveats:

  1. The connection is not getting reset when using the SDE database connection that I currently have indicated. I know this connection works because I can use the same connection to manually reset a layer's datasource in ArcPro.
  2. I force the connection to get set WITHOUT validation (the last False parameter). This results in "broken" layer connections. Though this is not the prefered method, at this point I can open the ProProject and then select the "red-exclamation" broken layer for one and point the layer to the new desired database connection. The good news is that this fixes all other broken connections provided that it found the "same" data layers in the new database connection.
  3. This one is an interesting find: the only layers in my ArcPro Map Project that get reset to "broken" are the ones that inside a Group Layer. Any layers in a Map that are NOT in a Group Layer are ignored. The print inside the for each layer loop shows that I see the layer and this it indeed has a connectionproperty that I checking (lyr.supports("CONNECTIONPROPERTIES")) for before calling the updateConnectionProperties. For all the layers that are not in a group, I still need to manually set the layer datasource in the ArcPro project because it does not break the connection or even change it. (this does not seem to me like expected behavior)

 

Questions:

  1. Has anyone successfully used the updateConnectionProperties method to change from one database connection to another?
  2. Am I using this class correctly? Layer.updateConnectionProperties ...
  3. I see that ArcPy also has an updateConnectionProperties  for the entire APRX project. According to the ESRI documenation (above) I see the following "You have control of updating data sources for individual layers or tables, or you can update all the layers or tables in a common workspace at once" And all the sample look similar to what I have.

 

Thought, comments, suggestions are greatly appreciated.

 

Thanks,

 

Diana

Outcomes