This has to be an idea already and I just can't find it. I would like to be able to use Python to edit a version on branch versioned data sets. Right now, the only way I can see to script out batch editing is to write the edits to DEFAULT. That causes database bloat that could be reduced by editing on a version and then post/reconciling programmatically.
@AmyRoust
To edit ArcGIS Enterprise branch version feature services via Python, you typically use the ArcGIS API for Python, which provides tools to interact with services hosted on ArcGIS Enterprise, including versioned feature services.
Working with Branch Versioning | ArcGIS API for Python
from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
# Connect to your portal
gis = GIS("https://your-portal-url.com/portal", "username", "password")
# Access the feature service
flc = FeatureLayerCollection("https://your-portal-url.com/arcgis/rest/services/YourService/FeatureServer", gis)
# Create or switch to a branch version
version_name = "Marcelo_Edit_Version"
version = flc.manager.create_version(version_name, description="Editing version for updates")
# Access the specific layer
layer = flc.layers[0] # assuming you want the first layer
# Set the version context
layer.version = version_name
# Query features to edit
features = layer.query(where="OBJECTID = 1", out_fields="*", return_geometry=True).features
# Modify attributes or geometry
features[0].attributes["Status"] = "Updated"
# Apply edits
layer.edit_features(updates=features)
There are multiple ways to edit a branch versioned dataset. All editing of branch versioned data must happen through its feature service. Publishing the data as a by reference service and then using either the ArcGIS API for python as @MarceloMarques has suggested, or arcpy are both acceptable. Below is an example of how you would edit using arcpy instead of the ArcGIS API for Python.
Since there are multiple ways to achieve the requested functionality, I will close this as already offered.
# User will need to sign in using their preferred method
import arcpy
server = << your branch versioned feature server url >
version_name = 'new_version_aa'
# Create a new version using the feature server
arcpy.management.CreateVersion(server, 'sde.DEFAULT', version_name)
# Create a Feature Layer
# Just assuming its the 0 indexed layer here for ease of example
new_lyr = arcpy.management.MakeFeatureLayer(f'{server}/0', 'test_lyr')
# Get the full version name
# Quick way to grab the newly created version name since it may contain more
# data, like the username, than was passed to CreateVersion GP tool
full_version_name = [v.name for v in arcpy.da.ListVersions(server) if version_name in v.name][0]
# Switch to the new version
arcpy.management.ChangeVersion(new_lyr, 'BRANCH', full_version_name)
# Edit the layer with a da.InsertCursor
with arcpy.da.InsertCursor(new_lyr, ['Field']) as cursor:
cursor.insertRow([1000])
@SSWoodward and @MarceloMarques - Thank you! I have only dabbled in the ArcGIS API for Python and it didn't even occur to me to look there. I will pursue this path.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.