Hello, I am looking to enable an edit session for a particular versioned layer in an ESRI geodatabase. I have a script tool where you select a layer as a parameter, but I get the following error:
RuntimeError: Objects in this class cannot be updated outside an edit session
So my question is this. Am i able to enable an edit session for a given versioned geodatabase layer through Python scripting, or is there a way to enable an editing session for a layer through model builder before running the script?
The script I am attempting to run has this information listed at the beginning.
""" Assetic.CreateAssetsTbxScript.py Script to setup as a toolbox script for use in Model Builder create assets in Assetic from the selected features in ArcGIS layer Assume Model Builder model has applied a feature selection This example assumes script is run in an edit session Requires assetic_esri v1.0.1.4 or later """
Hi @SouthCoastGIS,
You can use:
import arcpy
from arcpy.da import Editor as Editing, UpdateCursor as Updating, InsertCursor as Inserting, SearchCursor as Searching
##with arcpy.da.Editor( Workspace/database ) as edit:
## with ( Searching( ), Inserting( ) , Updating( ) ) as cursor:
## for row in cursor:
## logic handeling
## cursor.updateRow/cursor.insertRow( row )
There is plenty of documentation on editor if you research into it.
In this case, since you are pointing at a layer input by the user, I would suggest that the user have the database connection pointed to the correct version when it's input. If not, you could either create a new database connection (or connection string) pointed to the version you want.
Or you can try useing Change Version.
There is currently only the default version of the database so the sde version should suffice. Is there a method to take the layer name from the input parameter?
Apologies i am new to Python. I can see there is the arcpy.GetParameter() method, is there a way in ArcGIS Pro I could play around the GetParameter or GetParameterAsStrings methods interactively to see what is returned from my chosen inputs?
Using arcpy.GetParameter() returns the object that was input for that parameter when running the tool; whatever you have set as the parameter datatype. In the case of your tool here, if it isGPFeatureLayer or GPLayer, the return value of GetParameter() will be a Layer object so you'll have access to all that object's properties and methods. connectionProperties, dataSource, longName, or name might be the property you're looking for.
layer = arcpy.GetParameter(0)
def main(layer):
arcpy.AddMessage(f"connectionProperties: {layer.connectionProperties}")
arcpy.AddMessage(f"dataSource: {layer.dataSource}")
arcpy.AddMessage(f"longName: {layer.longName}")
arcpy.AddMessage(f"name: {layer.name}")
if __name__ == "__main__":
main(layer)