Select to view content in your preferred language

How to "Set Data Source" for a layer using arcpy?

344
1
03-04-2024 03:41 PM
Labels (1)
uasdev
by
New Contributor

Hi all,

I am trying to Set Data Source... using arcpy. This is required for my project, as I need to loop through many layers/project files.

Here is a simplified example of my code:

```

import arcpy, os
 
layerName = 'Site'
# old data source
# old_data_source = r'C:\path\to\Old.gdb\RealEstate\Site'
# new data source
new_data_source = r'C:\path\to\New.gdb\RealEstate\Site'

aprx = arcpy.mp.ArcGISProject(r'Current')

for m in aprx.listMaps():
    for lyr in m.listLayers():
        if lyr.supports("DATASOURCE"):
            if lyr.longName == layerName:
                lyr.updateConnectionProperties(lyr.dataSource, new_data_source)
```
 
I have seen in other posts to use connectionProperties of the lyr. But this requires alot of fine tuning to deal with different types of files (from geodatabase, to shapefile, to kml files).
Is there a simple way to Set Data Source using arcpy, as one would do simply using the GUI:
uasdev_0-1709595591222.png

 

0 Kudos
1 Reply
GeeteshSingh07
Occasional Contributor II

@uasdev I haven't personally done this but a long time back someone suggested me this course: https://www.esri.com/training/catalog/6361614a4b223839523bc32b/python-scripting-repairing-data-sourc...

Maybe this helps?

0 Kudos