I have tried to write a script to save a copy of a aprx, while changing the default gdb, using the documentation I found at the link below.
aprx.defaultGeodatabase = ....
https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/arcgisproject-class.htm
However, this doesn't actually work and the resulting aprx just has the same default gdb as the original aprx I have open. Is there something I'm missing?
import arcpy, os
from arcpy import env
proj_folder = arcpy.GetParameterAsText(0)
outfolder = arcpy.GetParameterAsText(1)
aprx_name = arcpy.GetParameterAsText(2)
aprx = arcpy.mp.ArcGISProject("CURRENT")
new_gdb = proj_folder + "\\Data\\ProjComp\\ProjComp.gdb"
aprx.defaultGeodatabase = new_gdb
aprx.saveACopy(outfolder+"\\"+aprx_name)
There was a cavaet in the link
defaultGeodatabase
(Read and Write)
The project's default geodatabase location. The string must include the geodatabase's full path and file name. Note: this property can be set as read-only by an Administrator and therefore it can't be modified.
String
ArcGISProject example 1 shows basically what you have done above, but I wonder if you have to make outfolder and proj_folder the same so that the aprx and gdb are in the same relative positions as the "CURRENT" project
Thanks, it seems like this yields the same results
I have had the same experience. The default geodatabase can be set/defined but once you open the aprx you need to add the geodatabase as a database connection. It will then show as the default geodatabase. This is the only workaround I presently am familiar with.
Thanks, I take it there's no way to add a database connection in arcpy then?
Not that I am aware of.
There is updateFolderConnections in arcpy's mapping class, but it doesn't look like you can simply add a folder you have to replace the existing connections dictionary with a replacement
ArcGISProject—ArcGIS Pro | Documentation
scroll wayyyyy down.