Hello,
I'm trying to automate a process for my collegues with ArcPy. A part of this process is to change the datasource of a single layer in a map to a new File Geodatabase. After searching the documentation i found this would be possible by using the updateConnectionProperties. I've been trying to get the following script to work, but no success so far:
import arcpy
# parameters
path = r"C:\MyProjects\ProjectX"
project = r"Kadastraal.aprx"
aprx = arcpy.mp.ArcGISProject(path+"\\"+project)
mapx = aprx.listMaps("Vastgoedobject1")[0]
layer = mapx.listLayers("Vastgoedobject")
#Change datasource of specified layer
old_file = r"C:\Data\OldData.gdb"
new_file = r"C:\NewData\newData.gdb"
for lyr in layer:
print (lyr.connectionProperties)
print ("Start updating datasource: " + lyr.name)
lyr.updateConnectionProperties({'dataset': 'Percelen_poly',
'workspace_factory': 'File Geodatabase', 'connection_info': {'database':
old_file}}, {'dataset': 'Percelen_poly', 'workspace_factory': 'File
Geodatabase', 'connection_info': {'database': nieuw_bestand}})
print("Datasource of " + lyr.name + " is updated")
print (lyr.connectionProperties)This gives no errors for me and if i look at the print statements it seems to update the source. But if i look at the map it still shows the old connection.
The only way i have found it works is if i add aprx.saveACopy (r"C:\Temp"). Then the connection is updated in the new aprx file. I tried using aprx.save() instead. but that just gives met the below error. Is this a known issue? Or am i doing something wrong?
OSError Traceback (most recent call last)
In [19]:
Line 22: aprx.save()
File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py, in save:
Line 333: return convertArcObjectToPythonObject(self._arc_object.save(*gp_fixargs((), True)))
OSError: C:\MyProjects\ProjectX\Kadastraal.aprx