HOW TO
The data source of a layer in a project references the feature class from the geodatabase physical location on a machine. The dataset can also reference a URL if the dataset is published as an ArcGIS Server service or an ArcGIS Online service. In the ArcPy module, the data source is represented in the 'connectionProperties' dictionary as shown below.
In the geodatabase
{'connection_info': {'database': 'E:\\ArcGIS\\Project\\Database.gdb'}, 'dataset': 'layerXYZ', 'workspace_factory': 'File Geodatabase'}
In ArcGIS Server Service
{'connection_info': {'url': 'http://sampleserver6.arcgisonline.com/arcgis/rest/services/911CallsHotspot/MapServer'}, 'dataset': '1', 'workspace_factory': 'FeatureService'}
To use the 'connectionProperties' dictionary, import the ArcPy and 'pprint' modules.
The steps below describe how to change the data source of a layer from referencing a geodatabase to a service URL.
import arcpy, pprint
aprx=arcpy.mp.ArcGISProject('current') map=aprx.listMaps()[0] l=map.listLayers()[0]
pprint.pprint(l.connectionProperties)
Find_dic = {'connection_info':{'database': 'C:\\arcgis\\TEST\\Editing\\Test.gdb'},'dataset': 'Springs','workspace_factory': 'File Geodatabase'}
replace_dic = {'connection_info':{'url':r'https://services.arcgis.com/Wl7Y1m92PbjtJs5n/arcgis/rest/services/None/FeatureServer'},'dataset': '0','workspace_factory': 'FeatureService'}
l.updateConnectionProperties(Find_dic,replace_dic,True,False,False) aprx.save()
The following shows the full script.
import arcpy, pprint aprx=arcpy.mp.ArcGISProject('current') map=aprx.listMaps()[0] l=map.listLayers()[0] pprint.pprint(l.connectionProperties) Find_dic = {'connection_info':{'database': 'C:\\arcgis\\TEST\\Editing\\Test.gdb'},'dataset': 'Springs','workspace_factory': 'File Geodatabase'} replace_dic = {'connection_info':{'url':r'https://services.arcgis.com/Wl7Y1m92PbjtJs5n/arcgis/rest/services/None/FeatureServer'},'dataset': '0','workspace_factory': 'FeatureService'} l.updateConnectionProperties(Find_dic,replace_dic,True,False,False) aprx.save()
The connectionProperties dictionary can also update the service URL. The steps below describe how to do so.
conProp = l.connectionProperties
conProp['connection_info']['url'] = r'https://sampleserver6.arcgisonline.com/arcgis/rest/services/EmergencyFacilities/FeatureServer'
l.updateConnectionProperties(l.connectionProperties,conProp) aprx.save()
The following shows the full script.
import arcpy, pprint p = arcpy.mp.ArcGISProject(r'C:\Projects\YosemiteNP\Yosemite.aprx') m = p.listMaps()[0] l = m.listLayers()[0] pprint.pprint(l.connectionProperties) conProp = l.connectionProperties conProp['connection_info']['url'] = r'https://sampleserver6.arcgisonline.com/arcgis/rest/services/EmergencyFacilities/FeatureServer' l.updateConnectionProperties(l.connectionProperties,conProp) aprx.save()
Article ID: 000021613
Get help from ArcGIS experts
Download the Esri Support App