Select to view content in your preferred language

AttributeError when using updateConnectionProperties

304
3
07-31-2024 07:17 PM
IT_AppLayers
New Contributor

I'm encountering an AttributeError when calling updateConnectionPropertieson a layer. 

This error is also inconsistent: 

  1. It appears sporadically. I've run the tool with the exact same inputs and it has worked. Then, I'll run the tool again, and it will give me this error.
  2. I've also tried iteratively calling the method on copies of the layer within a for loop, using the same connection properties dictionary and new connection properties dictionary. It may succeed for a few times before failing. 

There is also no descriptive error message (it just says AttributeError). 

I've verified that:

  • The layer object is of type arcpy._mp.Layer
  • updateConnectionProperties is listed among the object's valid attributes and methods

IT_AppLayers_1-1722478560328.png

Any help would be appreciated. 

Tags (2)
0 Kudos
3 Replies
AlfredBaldenweck
MVP Regular Contributor

Is it maybe a group layer or something?

0 Kudos
dslamb2022
Occasional Contributor

I've been running into a similar issue. At first I thought it was the upgrade from 3.2.0 to 3.2.2, because my code worked before. However, the solution for me was to update the connection properties on the layer file. I'm working within a toolbox within the project, and I don't know if this will work if you are running a script outside the project.

 

proj = arcpy.mp.ArcGISProject("CURRENT")
current_map = proj.createMap("Name for the map")
current_map.openView()
lyrx = arcpy.mp.LayerFile(path to layer file)
_layer = current_map.addLayer(lyrx)[0]
cp = {'dataset': name of the feature class, 'workspace_factory': 'File Geodatabase', 'connection_info': {'database': path to the file geodatabase}}
lyrx.updateConnectionProperties(None, cp)
_layer = current_map.addLayer(lyrx)[0]
_layer.name = nm

 

 

MAmbrosini
Emerging Contributor

I was also having this issue, and selecting the layer by using listLayers() instead of defining it when added solved it!