I have a python script that opens an edit session on a feature layer and updates a field. It runs perfectly fine from within ArcMap, but after I publish it as a Geoprocessing Service and try to run in from a web app, it throws up an error saying "Objects in this class cannot be updated outside an edit session".
I understand the error, but the script DOES open an edit session.
The feature class has attachments enabled and is not versioned.
Attached is a screenshot of the error from the ArcGIS Server Manager from when I try to run the script from the webpage.
The python script is:
import arcpy
arcpy.env.workspace = "Database Connections\\GISMO.sde"
Jobs = "GISMO.DBO.Jobs"
JobGuid = '{55729082-574F-46FC-89DE-C9CCC2B4AD96}'
edit = arcpy.da.Editor(arcpy.env.workspace)
edit.startEditing(False, False)
edit.startOperation()
whereClause = '"GlobalID" = ' + "'%s'" %JobGuid
with arcpy.da.UpdateCursor(Jobs, ["JobStatus"], whereClause) as JobCursor:
for row in JobCursor:
row[0] = 'X'
JobCursor.updateRow(row)
edit.stopOperation()
edit.stopEditing(True)
Any help here would be much appreciated.
Thanks,
-Paul
Solved! Go to Solution.
For anyone reading this, here is the current work-around, thanks to Keven from Esri. It appears that, while the original code I posted works fine within ArcMap, the process of deploying as a service doesn't work - in Kevin's words: "Its an issue in the way we parse the script when it gets moved to ArcGIS Server. Some part of the process gets confused, but using the os.path.dirname for some reason keeps it all in line.... However, moving the arcpy.env.workspace command and changing the way it gets to the path seems to allow it to work correctly. "
Here is the code that worked, you'll notice the connection string is slightly different, and the workspace line uses os.path.dirname:
import os, arcpy newText = arcpy.GetParameterAsText(0).strip() testHasAttachments = r"Database Connections\TEMPDATA.sde\tempdata.DBO.testhasattachments" arcpy.env.workspace = os.path.dirname(testHasAttachments) edit = arcpy.da.Editor(arcpy.env.workspace) edit.startEditing(False, False) edit.startOperation() with arcpy.da.UpdateCursor(testHasAttachments, ["Description", "myIDField"]) as ACursor: for a in ACursor: a[0] = newText arcpy.AddMessage(str(a[1]) + " updated successfully.") ACursor.updateRow(a) edit.stopOperation() edit.stopEditing(True)
Regards,
-Paul
does Database Connections\\GISMO.sde exist on on the server ? It may be helpful to see the error message also.
HI Dallas - thanks for the reply.
Yes the connection exists on the server. For example. there is another feature layer called Properties (that does not have attachments enabled) that exists in the same dataset alongside this 'Jobs' one, and the same code will update a field on the Properties layer no problem.
the error message can be viewed in the png I attached to the original message (error.png).
Cheers
-Paul
For anyone reading this, here is the current work-around, thanks to Keven from Esri. It appears that, while the original code I posted works fine within ArcMap, the process of deploying as a service doesn't work - in Kevin's words: "Its an issue in the way we parse the script when it gets moved to ArcGIS Server. Some part of the process gets confused, but using the os.path.dirname for some reason keeps it all in line.... However, moving the arcpy.env.workspace command and changing the way it gets to the path seems to allow it to work correctly. "
Here is the code that worked, you'll notice the connection string is slightly different, and the workspace line uses os.path.dirname:
import os, arcpy newText = arcpy.GetParameterAsText(0).strip() testHasAttachments = r"Database Connections\TEMPDATA.sde\tempdata.DBO.testhasattachments" arcpy.env.workspace = os.path.dirname(testHasAttachments) edit = arcpy.da.Editor(arcpy.env.workspace) edit.startEditing(False, False) edit.startOperation() with arcpy.da.UpdateCursor(testHasAttachments, ["Description", "myIDField"]) as ACursor: for a in ACursor: a[0] = newText arcpy.AddMessage(str(a[1]) + " updated successfully.") ACursor.updateRow(a) edit.stopOperation() edit.stopEditing(True)
Regards,
-Paul