Python GP service to add attachments

03-20-2017 10:40 AM
New Contributor

Hi Everyone,

I am working on a python GP service that will allow a user to add a file attachment to a table record.  I can do this the manual way of selecting a site, selecting its related record table, hitting edit to create a new record, and then adding an attachment. I would like to set it up so that you can just click on the widget, input attributes and the file to upload, and do it all in one shot. 

I have  a "Sites" feature class, and a "Documents" related table (1:M, key is the site name).  The "Documents" table stores the attributes of the document (author, name of document, etc), and has attachments enabled..  This related table will allow for a little better querying capability off document attributes.  The fc and table are a postrges db. 

I was able to get the first part of the script to work as a ervice - in being able to add a new record to the "documents" table.  When I added the next part of adding the attachment it works fine in desktop, but when executed in Web App Builder, the error in server log is ", in edit= arcpy.da.Editor(GDBToEdit) RuntimeError: cannot open workspace Failed to execute (AddDocRecordandAttach). Failed to execute (AddDocRecordandAttach)"

This is strange, as in the first part of the script this is the same database connection path (had to put the .sde file in a folder accessible to the server).  I am just not sure where to check that maybe a lock is set in the first part that is not getting cleared in the second part, or I am not starting/stopping the edit session in the right place. Thanks for your help, and please, if anyone can point out how to insert the code nicely in this question, I would appreciate it

First Part

import arcpy, sys, traceback
arcpy.env.overwriteOutput= True

formname = sys.argv[1]
sdsfeaturename = sys.argv[2]
doctitle = sys.argv[3]
creatorname = sys.argv[4]
approvername = sys.argv[5]
approvedate = sys.argv[6]
uploaddate = sys.argv[7]
filename = sys.argv[8]
mimetype =sys.argv[9]
filetype = sys.argv[10]
filepath = sys.argv[11]

GDBToEdit = "C:\ArcGIS Server Installation Files\\Connection to XXXXX[gis590_sde]dude].sde"
tbl = "C:\ArcGIS Server Installation Files\\Connection to XXXXXXXXXX[gis590_sde]dude].sde\\gis590_sde.dude.tbl_documents"

edit= arcpy.da.Editor(GDBToEdit)
edit.startEditing(False, False)
# formid to objectid
fieldList = ["formname", "sdsfeaturename", "doctitle", "creatorname", "approvername", "approvedate", "uploaddate", "filename", "mimetype", "filetype", "documentdata", "formid"]

valuesList = [formname, sdsfeaturename, doctitle, creatorname, approvername, approvedate, uploaddate, filename, mimetype, filetype, "",""]

cursor = arcpy.da.InsertCursor(tbl, fieldList)
del cursor
del cursor


Second Part

#Create a match table
#matchtabletemplate = "C:\\ArcGIS Server Installation Files\\Connection to xxxxxx[gis590_sde]dude].sde\\gis590_sde.dude.tbl_matchtable"

matchtable = arcpy.CreateTable_management ("in_memory", "matchtable1")
arcpy.AddField_management(matchtable, "MATCHID", "LONG")
arcpy.AddField_management(matchtable, "FILENAME", "TEXT", field_length=100)

#Search cursor to get formid{MATCHID} for new record
whereclause = "formname = '{0}' AND sdsfeaturename = '{1}'".format(formname, sdsfeaturename)
fields = ["MATCHID", "FILENAME"]

with arcpy.da.SearchCursor(tbl, "formid", whereclause) as Scursor:
with arcpy.da.InsertCursor(matchtable, fields) as Icursor:
for row in Scursor:
Icursor.insertRow((row[0], filepath))

intable = "C:\ArcGIS Server Installation Files\\Connection to XXXXX[gis590_sde]dude].sde\\gis590_sde.dude.tbl_documents"

inmatchfield = "formid" #formid
#inmatchtable = "matchtable1"
matchjoinfield = "MATCHID"
matchfilefield = "FILENAME"
arcpy.AddAttachments_management(intable, inmatchfield, matchtable, matchjoinfield, matchfilefield)

0 Kudos
0 Replies