Hi, I recently wrote a small script that takes all the layer sourcing in the currently open map (ArcPro 2.5.1) over to a new database contained in enterprise. I have several team members that also need to use this tool as we migrate from one db to another and republish our templates in the new enterprise portal. I thought a webtool would be the best way to share this tool rather than emailing the code out and showing each member how to set up the ArcPro Script tool. The publishing of the tool worked great, but when I pull it in I keep getting an error when I try to reference the following:
aprx = arcpy.mp.ArcGISProject('CURRENT')
map_ = aprx.activeMap
This code works if the script tool is run locally without error but when the gp tool is pulled into pro from portal and run from there, I get the following error:
Do I have to explicitly request for a project as a parameter as well as a map document? Or is there something I am missing between running a script tool locally vs over portal?
Full code is below:
import arcpy
aprx = arcpy.mp.ArcGISProject('CURRENT')
map_ = aprx.activeMap
for layer in map_.listLayers():
conProps = layer.connectionProperties
conProps['connection_info']['db_connection_properties'] = '<mydatabase>'
conProps['connection_info']['instance'] = 'sde:postgresql:<mydatabase>'
conProps['connection_info']['server'] = '<mydatabase>'
layer.updateConnectionProperties(layer.connectionProperties, conProps)
aprx.save()
This is a great idea. Unfortunately, the way web tools work doesn't allow access to your current ArcGIS project. The process runs on the enterprise machine, so it needs a reference to the .aprx files from that machine. So, no arcpy.mp.ArcGISProject('CURRENT') in web tools.
Here's an alternative you could use if you and your team have a shared network drive that the enterprise server could access.
import arcpy
# add .aprx file location
aprx_file_location = arcpy.GetParameterAsText(0)
#aprx = arcpy.mp.ArcGISProject('CURRENT')
aprx = arcpy.mp.ArcGISProject(aprx_file_location)
for map_ in aprx.listMaps():
for layer in map_.listLayers():
conProps = layer.connectionProperties
conProps['connection_info']['db_connection_properties'] = '<mydatabase>'
conProps['connection_info']['instance'] = 'sde:postgresql:<mydatabase>'
conProps['connection_info']['server'] = '<mydatabase>'
layer.updateConnectionProperties(layer.connectionProperties, conProps)
aprx.save()
You could possibly expand this using arcpy.da.Walk to fix all .aprx files in a shared folder.