ArcPy Layer updateConnectionProperties not working as expected

12179
19
Jump to solution
05-16-2019 03:09 PM
DianaBenedict1
New Contributor III

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

19 Replies
DianaBenedict1
New Contributor III

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

0 Kudos
JorgeMorteo
New Contributor III

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

					
				
			
			
				
			
			
				
			
			
			
			
			
			
		
0 Kudos
JeffMoulds
Esri Contributor

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? 

0 Kudos
JorgeMorteo
New Contributor III

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.

0 Kudos
ChrisHolmes
Occasional Contributor III

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

0 Kudos
BrianCunningham2
New Contributor III

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.

0 Kudos
JackFanZhang
Occasional Contributor

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.

 

0 Kudos
KimOllivier
Occasional Contributor III

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!

RutgerDujardin
New Contributor

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.

 

0 Kudos
RutgerDujardin
New Contributor

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

0 Kudos