Hello,
I am trying to update connection properties for all layers in a Pro project map that are connected to our admin database to a read-only version of that database. When I run the following script, it runs but the database connections do not get updated.
import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
for m in aprx.listMaps("Mobile_Map"):
for lyr in m.listLayers():
if not(lyr.isGroupLayer or lyr.isBasemapLayer):
try:
lyr.updateConnectionProperties(current_connection_info="A:\DBConnection\admin.sde", new_connection_info="A:\DBConnection\dr.sde")
except:
print("Not an sde database or the database connection does not need to be updated.")
Solved! Go to Solution.
I would recommend using the supports() method on layer to check if it has the property "connectionProperties"
for lyr in m.listLayers():
if lyr.supports("connectionProperties"):
print(lyr.connectionProperties["connection_info"])
lyr_update = lyr.connectionProperties
lyr_update['connection_info']['password']='<password>'
lyr_update['connection_info']['user']='dr'
lyr.updateConnectionProperties(lyr.connectionProperties, lyr_update)
Edit: the code above works when I change '<password>' to 'password'. Thanks @BlakeTerhune ! Much appreciated.
I think you need to save the project when you're done.
Thank you for the suggestion. I just tried it but it still does not work.
I use something similar to this.
proj_gdb = #SDE Source
for lyr in m.listLayers():
lyr_update = lyr.connectionProperties
lyr_update['connection_info']['database'] = proj_gdb
lyr_update['dataset'] = #SDE layer name
lyr.updateConnectionProperties(lyr.connectionProperties, lyr_update)
Edit: In this example, I'd probably get the source feature class name and, assuming that the target has the same name as the source, use that variable to get the correct target feature class name.
Thank you for this--it makes sense to me. I just tried something similar (see code in .txt doc) and it runs but still doesn't work properly. I had to modify the code some since I'm moving the connection from between different users of the same SDE database--the only difference that I can see when printing the database properties of the dr vs. admin database is the user and password. So, I tried to just update those in the code this time...
I could see some issues with that code if you change the user/password and the user doesn't have access to the relevant layer. Also if the table has a different name than the current layer, since it'll be looking for the same table name, but with a different connection. I'd recommend taking a look at the source tab in the properties for both the original and the new tables and see if there's any differences that will need to be updated to match beyond the database login. For example if the table name is different such as DBO.layername and User.layername_1, different feature dataset name, different instance name, server ip/hostname.
Thanks for this suggestion but everything is the exact same aside from the un/pw and permissions set on each. Same database version and everything--just one user has admin permissions and the other user was created for viewing only.
I would recommend using the supports() method on layer to check if it has the property "connectionProperties"
for lyr in m.listLayers():
if lyr.supports("connectionProperties"):
print(lyr.connectionProperties["connection_info"])
lyr_update = lyr.connectionProperties
lyr_update['connection_info']['password']='<password>'
lyr_update['connection_info']['user']='dr'
lyr.updateConnectionProperties(lyr.connectionProperties, lyr_update)
Thanks, Blake. I tried your method using the code below--it ran but did not change the database connection. Please let me know if you have another suggestion or see any issues with my code.
import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
for m in aprx.listMaps("Mobile_Map"):
for lyr in m.listLayers():
if lyr.supports("connectionProperties"):
try:
print(lyr.connectionProperties["connection_info"])
lyr_update = lyr.connectionProperties
lyr_update['connection_info']['password']='<password>'
lyr_update['connection_info']['user']='dr'
lyr.updateConnectionProperties(lyr.connectionProperties, lyr_update)
except:
print("Not an sde database or the database connection does not need to be updated.")
aprx.save()
del aprx
Edit: the code above works when I change '<password>' to 'password'. Thanks @BlakeTerhune ! Much appreciated.