HOW TO

Change the data source of a layer from referencing a geodatabase to an ArcGIS Server Service URL in ArcGIS Pro using ArcPy

Last Published: May 20, 2021

Summary

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'}

Procedure

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.

  1. Import the necessary modules.
import arcpy, pprint
  1. Specify the project details.
aprx=arcpy.mp.ArcGISProject('current')
map=aprx.listMaps()[0]
l=map.listLayers()[0]
  1. Call the connectionProperties dictionary.
pprint.pprint(l.connectionProperties)
  1. Specify the current data source (geodatabase) dictionary.
Find_dic = {'connection_info':{'database': 'C:\\arcgis\\TEST\\Editing\\Test.gdb'},'dataset': 'Springs','workspace_factory': 'File Geodatabase'}
  1. Specify the service URL dictionary.
replace_dic = {'connection_info':{'url':r'https://services.arcgis.com/Wl7Y1m92PbjtJs5n/arcgis/rest/services/None/FeatureServer'},'dataset': '0','workspace_factory': 'FeatureService'}
  1. Call the updateConnectionProperties() function to update the data source, and save the project file.
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.

  1. Follow Steps 1 through 3 from the section above.
  2. Define a parameter to store the dictionary.
conProp = l.connectionProperties
  1. Update the URL.
conProp['connection_info']['url'] = r'https://sampleserver6.arcgisonline.com/arcgis/rest/services/EmergencyFacilities/FeatureServer'
  1. Call the updateConnectionProperties() function to update the data source, and save the project file.
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

Software:
  • ArcGIS Pro

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options