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:
Questions:
Thought, comments, suggestions are greatly appreciated.
Thanks,
Diana
Solved! Go to Solution.
Thank you for reaching out to this Thread to let your users know of the enhancement to the arcpy.mp updateConnectionProperties. This functionality will surely simplify our code.
Regards
Diana
I used the
aprx.updateConnectionProperties in ArcGIS PRO 2.6 runs very slow as it goes through all
the layers that need to be change. It takes about four minutes each layer and at some point
it does not give me error and just seats there spinning. In ArcMap was a 100 times faster.
import arcpy import sys CASE = sys.argv[1] aprx = arcpy.mp.ArcGISProject("CURRENT") aprx.updateConnectionProperties(r"C:\GIS_WorkSpace\Geodatabase\CASES\Shapes", r"X:\PD_Notify\Labels-2020\\" + CASE) del aprx
Jorge, thanks for the feedback. Yes, we know in some cases referencing a project via CURRENT and executing updateConnectionProperties is slow. You can speed up the time by setting the validate parameter in updateConnectionProperties to False. It is also faster if you run the script out of the app. That being said, I havent seen speeds as slow as 4 mins per layer. What type of data are the layers?
They are shapefiles. I wonder if that is the problem. Just to test, I am going export some of the shapefiles to Geodatabase and see if that is the problem.
Hi Jeff,
Does updateConnectionProperties require that feature class name in old and new be the same?
I have the situation where we have moved a number of feature classes from file geodatabase to sde, but the feature classes have also been renamed in sde to conform to corporate standards. So, for example:
before: Data.gdb\roads; after: database.sde\transportation
before: Data.gdb\rivers; after: database.sde\hydrology
Is it possible to fix these broken links using updateConnectionProperties. Seemed pretty easy to do this in arcMap, just starting to see how/if it will be possible to accomplish the same in Pro.
thanks
I too was running into the same issue where the data source was not getting changed when using a .sde file.
I changed tactics and did as Alberto Aloe suggested and it worked like a charm. I even noticed that the script was running a little slower as it churned through each layer in each map, so I could tell that it was doing something.
Though share my experience here. I had the same issue where using updateConnectionProperties(l.connectionProperties, newConnectionProperties) on updating Layer data source from one FileGDB to another FileGDB didn't work. the process returned successful message without any warning. Couldn't work out why for a while. After adding validate=false and run again, the layer became broken, and that's because the new FileGDB doesn't have the same feature class that the layer referred to. After sorting the feature class issue, the function works as expected.
The help doc says
Validate: If set to True, the connection properties will only be updated if the new_connection_info value is a valid connection. If it is not valid, the connection will not be replaced.
I would thought it would be necessary to improve the updateConnectionProperties function that when validate is True, and if the new connection is invalid, it should return a warning to make the user's attention.
Here I am in 2022 with ArcGIS Pro 3.01 installed still looking to see why it does not work with a couple of simple file geodatabases!
Hi,
I'm trying as well to use the updateConnectionProperties function. Just in its most basic form, linking a new file geodatabase to an existing .aprx. Old and new databases have exactly the same structure. In my case, the function changes correctly the path's within the .aprx file. But when I open the .aprx it gives my only broken layer connections. When I click the red-exclamation mark and direct to exactly the same location/feature class it retrieves the data.
My question: Why does the red-exclamation mark appears if the path is set correctly? Why can't the .aprx file represent the data when opening it?
The code I'm using:
aprx = arcpy.mp.ArcGISProject(path_Template_aprxFile)
aprx.updateConnectionProperties(None, path_newDatabase)
aprx.saveACopy(path_New_aprxFile)
If anyone can help solving my problem, be welcome.
Applied strategy to solve my problem. Updating the connection for each layer separately:
aprx = arcpy.mp.ArcGISProject(path_Template_aprxFile)
for m in aprx.listMaps():
for layer in m.listLayers():
newCP = layer.connectionProperties
newCP['connection_info']['database'] = path_newDatabase
layer.updateConnectionProperties(layer.connectionProperties, newCP)
aprx.saveACopy(path_New_aprxFile)