Trying to update data source for layers in ArcGIS Pro maps programmatically. So, I must be misunderstanding the syntax for doing that explained here. Because the following does not work:
aprx = arcpy.mp.ArcGISProject(r'path to my project file')
#This gets me list of all the maps
maplist = aprx.listMaps()
#If I only want those belonging to MyGroup
mygroup = aprx.listMaps('MyGroup')
#To get the layers in that group
grouplayers = mygroup.listLayers()
new_sdeConn = r'path to my SDE connection file'
for lyr in grouplayers:
old = lyr.connectionProperties #also tried lyr.connectionProperties['connection_info']
This doesn't throw an error. it just doesn't do update the data sources as desired. The ESRI example goes like:
Does this maybe only work for the project as a whole?
The issue you're having is that lyr.connectionProperties returns a dictionary but your new connection is a workspace path. You just need to access the right key in the dictionary:
old lyr connectionProperties['connection_info']['database']
lyr updateConnectionProperties old new_sdeConn
Stephen, thanks. Appreciate you chiming. I realize that connection_info is a dictionary. But I wasn't sure how to use it. Keyword "database' only updates the database. I'm actually trying to switch this to use a different account (new 'user' and 'password') and was hoping to do so using a new SDE connection file. Can you maybe not to do?
If you look at the other keys in the dict, I need more than 'database' to be updated.
I am not an expert on these SDE connection files. But my understanding has always been that they store a reference to DB server/instance, DB name, authentication (and/pr password). So basically what's in the dictionary above. So merely updating 'database' in the dict wouldn't be sufficient, I think. To be safe, I tried it, and to no effect.
Ah, right. I forgot that you're using SDE connections. To switch users I'd try modifying ['connection_info]['user'] and ['connection_info']['password'].
for lyr in layers:
old_conn = lyr.connectionProperties
new_conn = old_conn
new_conn['connection_info']['user'] = new_user
new_conn['connection_info']['password'] = new_pw
I've tried passing in a complete new dict or just the 'user' and 'password' values as you suggested. I've tried the approach of referencing another layer's connectionProperties suggested here:
lyr = m.listLayers('some other layer')
None of it is working. Darn!
Hm, maybe the credentials can't be modified in the way the other values can. I do notice that there aren't examples in the documentation that show changing users in the connection properties. I had some success changing a connection from an OS authentication connection to a DBMS connection, but I don't have multiple named DB users to test changing credentials.
Sadly I have given up banging my head against the desk trying to make this work. I have searched through three other threads on GeoNet of people having a variation of this same problem without resolution:
In the end I found an old ArcMap .mxd with the same data also pointing to the wrong database, used good ole reliable lyr.replaceDataSource() to fix the data sources, imported the mxd as a new map in Pro, then copied the data over into my original map. Sad but true.
As with so many other instances, not finding any info on how to do it, I abandoned this a while back. But I'm sure I will have to look at it again some day and will remember this post.
I have not tried to recreate my update SDE datasource scripts from ArcMap to Pro yet so I have not experienced this hardship yet, but I have found that the Set Data Source(s)... functionality of ArcCatalog has not been ported over to Pro. Although this ArcCatalog functionality does not work on batch mxds, I have found it very useful for moving publishing mxds from development to test to production where the SDE database is different for each environment.
How many Pro aprxs are you looking to update the SDE connection properties for?