Enable edit session for particular layer in a versioned geodatabase

176
4
2 weeks ago
NigelGriffithsAU
New Contributor

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 is as below.

"""
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
"""
import arcpy
import assetic_esri
import os

layer = arcpy.GetParameter(0)


def main(layer):
    """
    For the given layer's selected features
    create a corresponding asset in Assetic and update the feature with
    the asset friendly asset id, or the assetic internal asset guid.
    Assumes the xml config file has the layer name (as appears in the TOC)
    :param layer: the layer to process
    """
    # Initialise the assetic_esri library.  It will read the config files etc
    if not initasseticesri():
        return

    # initialise assetic esri tools for layers
    tools = assetic_esri.LayerTools()
    # Uncomment to following if using arcMap and you don't want messages to
    # go to pythonaddins.MessageBox(), assetic_esri instead uses
    # arcpy.AdMessage() to send messages
    #assetic_esri.config.force_use_arcpy_addmessage = True

    # get record count
    count = len(layer.getSelectionSet())

    # execute asset creation
    arcpy.AddMessage("Processing layer: {0}, {1} Selected Features".format(
        layer.name, count))
    tools.create_asset(layer)

    # uncomment if arcMap to reset message output in case addin is used next
    #assetic_esri.config.force_use_arcpy_addmessage = False


def initasseticesri():
    """
    initialise the helper module assetic_esri
    sets the paths to the xml config and ini files
    """
    appdata = os.environ.get("APPDATA")
    inifile = os.path.abspath(appdata + "\\Assetic\\assetic.ini")
    logfile = os.path.abspath(appdata + "\\Assetic\\addin.log")
    xmlfile = os.path.abspath(appdata + "\\Assetic\\arcmap_edit_config.xml")
    try:
        ae = assetic_esri.Initialise(xmlfile, inifile, logfile, "Info")
    except Exception as ex:
        arcpy.AddError("Error initialising assetic_esri: {0}".format(ex))
        return False
    return True


if __name__ == "__main__":
    main(layer)

 

0 Kudos
4 Replies
RPGIS
by
Occasional Contributor III

Hi @NigelGriffithsAU,

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.

0 Kudos
BlakeTerhune
MVP Regular Contributor

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.

0 Kudos
NigelGriffithsAU
New Contributor

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? 

0 Kudos
BlakeTerhune
MVP Regular Contributor

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)

 

0 Kudos