0

I tried to use the following code to update the connected database user for layers within an ArcGIS Pro project map. The code runs but it does not change the data sources from one enterprise geodatabase user to another as intended. I previously posted this issue to the GIS Community website but have yet to find an answer--here is the thread for that as well: https://community.esri.com/t5/python-questions/updateconnectionproperties-is-not-working/m-p/1333516#M68773

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):
            print(lyr.connectionProperties["connection_info"])
            try:
                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
1
  • Try making a copy of the lyr.connectionProperties dict, i.e. copy.deepcopy(lyr.connectionProperties). Alternatively, create a database connection (.sde file) with the updated info and pass that in to updateConnectionProperties.
    – user2856
    Oct 1, 2023 at 22:25

1 Answer 1

0

This following code below worked. See full discussion in the ESRI community forum (https://community.esri.com/t5/python-questions/updateconnectionproperties-is-not-working/m-p/1334613#M68815).

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 Exception as e:
                raise Exception("Not an sde database or the database connection does not need to be updated.") from e
aprx.save()
del aprx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.