Here is the code you need to edit a versioned SDE feature class. You may want to enable undo/redo in the line that reads edit.startEditing(False, True) by changing it to edit.startEditing(True, True).import arcpy
import os
import pythonaddins
class BlueBird(object):
"""Implementation for BlueBird.button (Button)"""
def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
# Assign string literal layer names to variables to make them easier to change with just one line of code.
# Good practice for template code that you may not want to do a search and replace to adapt it for new layers.
parcels = "Parcels"
addressPoints = "Address Points"
#Get the current map document and the first data frame.
mxd = arcpy.mapping.MapDocument('current')
df = arcpy.mapping.ListDataFrames(mxd)[0]
#Check to make sure user had at least one parcel selected.
parcelsCount = int(arcpy.GetCount_management(parcels).getOutput(0))
if 0 < parcelsCount < 2:
lyr = arcpy.mapping.ListLayers(mxd, addressPoints, df)[0]
workspace = lyr.workspacePath
# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(workspace)
# Edit session is started without an undo/redo stack for versioned data
# (for second argument, use False for unversioned data)
edit.startEditing(False, True)
# Start an edit operation
edit.startOperation()
#Select Address Points within selected parcel
arcpy.SelectLayerByLocation_management(addressPoints, "HAVE_THEIR_CENTER_IN", parcels)
with arcpy.da.SearchCursor(parcels, ['PIN','TMS']) as singleCursor:
for singleRow in singleCursor:
PIN = singleRow[0]
TMS = singleRow[1]
with arcpy.da.UpdateCursor(addressPoints, ['PIN', 'TMS']) as singleCursor:
for singleRow in singleCursor:
singleRow[0] = PIN
singleRow[1] = TMS
singleCursor.updateRow(singleRow)
# Stop the edit operation.
edit.stopOperation()
print('Parcel address update is complete.')
elif 1 < parcelsCount < 1000:
#Create in memory parcel layer
selectedParcels = arcpy.MakeFeatureLayer_management(parcels, "selectedParcels")
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr.name == addressPoints:
addressLayer = lyr
if lyr.name == parcels:
parcelsLayer = lyr
workspace = addressLayer.workspacePath
# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(workspace)
# Edit session is started without an undo/redo stack for versioned data
# (for second argument, use False for unversioned data)
edit.startEditing(False, True)
# Start an edit operation
edit.startOperation()
with arcpy.da.SearchCursor(parcels, ['PIN', 'TMS', 'OBJECTID']) as parcelCursor:
for parcelRow in parcelCursor:
PIN = parcelRow[0]
TMS = parcelRow[1]
OID = parcelRow[2]
OID = str(OID)
arcpy.SelectLayerByAttribute_management("selectedParcels", "NEW_SELECTION", "OBJECTID= "+OID)
#Select Address Points by location, within selected Parcels.
arcpy.SelectLayerByLocation_management(addressPoints,"HAVE_THEIR_CENTER_IN","selectedParcels")
with arcpy.da.UpdateCursor(addressPoints, ['PIN', 'TMS']) as addressCursor:
for addressRow in addressCursor:
addressRow[0] = PIN
addressRow[1] = TMS
addressCursor.updateRow(addressRow)
del PIN, TMS
# Stop the edit operation.
edit.stopOperation()
arcpy.Delete_management("selectedParcels")
print('Parcel address update is complete.')
else:
print('Please select a valid number of parcels.')