<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Python GP service to add attachments  in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-gp-service-to-add-attachments/m-p/682098#M52824</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Everyone,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am working on a python GP service that will allow a user to add a file attachment to a table record. &amp;nbsp;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have &amp;nbsp;a "Sites" feature class, and a "Documents" related table (1:M, key is the site name). &amp;nbsp;The "Documents" table stores the attributes of the document (author, name of document, etc), and has attachments enabled.. &amp;nbsp;This related table will allow for a little better querying capability off document attributes. &amp;nbsp;The fc and table are a postrges db.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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. &amp;nbsp;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 "&lt;SPAN style="color: #505051; background-color: #e5edf4; font-size: 12px;"&gt;, in &lt;/SPAN&gt;edit= arcpy.da.Editor(GDBToEdit) RuntimeError: cannot open workspace Failed to execute (AddDocRecordandAttach). Failed to execute (AddDocRecordandAttach)"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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). &amp;nbsp;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&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;First Part&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import arcpy, sys, traceback&lt;BR /&gt;arcpy.env.overwriteOutput= True&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;formname = sys.argv[1]&lt;BR /&gt;sdsfeaturename = sys.argv[2]&lt;BR /&gt;doctitle = sys.argv[3]&lt;BR /&gt;creatorname = sys.argv[4]&lt;BR /&gt;approvername = sys.argv[5]&lt;BR /&gt;approvedate = sys.argv[6]&lt;BR /&gt;uploaddate = sys.argv[7]&lt;BR /&gt;filename = sys.argv[8]&lt;BR /&gt;mimetype =sys.argv[9]&lt;BR /&gt;filetype = sys.argv[10]&lt;BR /&gt;filepath = sys.argv[11]&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;GDBToEdit = "C:\ArcGIS Server Installation Files\\Connection to XXXXX[gis590_sde]dude].sde"&lt;BR /&gt;tbl = "C:\ArcGIS Server Installation Files\\Connection to &lt;SPAN&gt;XXXXX&lt;/SPAN&gt;&lt;SPAN&gt;XXXXX&lt;/SPAN&gt;[gis590_sde]dude].sde\\gis590_sde.dude.tbl_documents"&lt;/P&gt;&lt;P&gt;edit= arcpy.da.Editor(GDBToEdit)&lt;BR /&gt;edit.startEditing(False, False)&lt;BR /&gt;edit.startOperation()&lt;BR /&gt;# formid to objectid&lt;BR /&gt;fieldList = ["formname", "sdsfeaturename", "doctitle", "creatorname", "approvername", "approvedate", "uploaddate", "filename", "mimetype", "filetype", "documentdata", "formid"]&lt;/P&gt;&lt;P&gt;valuesList = [formname, sdsfeaturename, doctitle, creatorname, approvername, approvedate, uploaddate, filename, mimetype, filetype, "",""]&lt;/P&gt;&lt;P&gt;cursor = arcpy.da.InsertCursor(tbl, fieldList)&lt;BR /&gt;try:&lt;BR /&gt; cursor.insertRow(valuesList)&lt;BR /&gt; del cursor&lt;BR /&gt;except:&lt;BR /&gt; traceback.print_exc()&lt;BR /&gt; del cursor&lt;/P&gt;&lt;P&gt;edit.stopOperation()&lt;BR /&gt;edit.stopEditing(True)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;Second Part&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#Create a match table &lt;BR /&gt;#matchtabletemplate = "C:\\ArcGIS Server Installation Files\\Connection to xxxxxx[gis590_sde]dude].sde\\gis590_sde.dude.tbl_matchtable"&lt;/P&gt;&lt;P&gt;matchtable = arcpy.CreateTable_management ("in_memory", "matchtable1")&lt;BR /&gt;arcpy.AddField_management(matchtable, "MATCHID", "LONG") &lt;BR /&gt;arcpy.AddField_management(matchtable, "FILENAME", "TEXT", field_length=100)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#Search cursor to get formid{MATCHID} for new record&lt;BR /&gt;whereclause = "formname = '{0}' AND sdsfeaturename = '{1}'".format(formname, sdsfeaturename) &lt;BR /&gt;fields = ["MATCHID", "FILENAME"]&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;with arcpy.da.SearchCursor(tbl, "formid", whereclause) as Scursor:&lt;BR /&gt; with arcpy.da.InsertCursor(matchtable, fields) as Icursor:&lt;BR /&gt; for row in Scursor:&lt;BR /&gt; Icursor.insertRow((row[0], filepath))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;intable = "C:\ArcGIS Server Installation Files\\Connection to XXXXX[gis590_sde]dude].sde\\gis590_sde.dude.tbl_documents"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;inmatchfield = "formid" #formid&lt;BR /&gt;#inmatchtable = "matchtable1"&lt;BR /&gt;matchjoinfield = "MATCHID"&lt;BR /&gt;matchfilefield = "FILENAME"&lt;BR /&gt;arcpy.AddAttachments_management(intable, inmatchfield, matchtable, matchjoinfield, matchfilefield)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 20 Mar 2017 17:40:01 GMT</pubDate>
    <dc:creator>EdwardParry1</dc:creator>
    <dc:date>2017-03-20T17:40:01Z</dc:date>
    <item>
      <title>Python GP service to add attachments</title>
      <link>https://community.esri.com/t5/python-questions/python-gp-service-to-add-attachments/m-p/682098#M52824</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Everyone,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am working on a python GP service that will allow a user to add a file attachment to a table record. &amp;nbsp;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have &amp;nbsp;a "Sites" feature class, and a "Documents" related table (1:M, key is the site name). &amp;nbsp;The "Documents" table stores the attributes of the document (author, name of document, etc), and has attachments enabled.. &amp;nbsp;This related table will allow for a little better querying capability off document attributes. &amp;nbsp;The fc and table are a postrges db.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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. &amp;nbsp;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 "&lt;SPAN style="color: #505051; background-color: #e5edf4; font-size: 12px;"&gt;, in &lt;/SPAN&gt;edit= arcpy.da.Editor(GDBToEdit) RuntimeError: cannot open workspace Failed to execute (AddDocRecordandAttach). Failed to execute (AddDocRecordandAttach)"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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). &amp;nbsp;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&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;First Part&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import arcpy, sys, traceback&lt;BR /&gt;arcpy.env.overwriteOutput= True&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;formname = sys.argv[1]&lt;BR /&gt;sdsfeaturename = sys.argv[2]&lt;BR /&gt;doctitle = sys.argv[3]&lt;BR /&gt;creatorname = sys.argv[4]&lt;BR /&gt;approvername = sys.argv[5]&lt;BR /&gt;approvedate = sys.argv[6]&lt;BR /&gt;uploaddate = sys.argv[7]&lt;BR /&gt;filename = sys.argv[8]&lt;BR /&gt;mimetype =sys.argv[9]&lt;BR /&gt;filetype = sys.argv[10]&lt;BR /&gt;filepath = sys.argv[11]&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;GDBToEdit = "C:\ArcGIS Server Installation Files\\Connection to XXXXX[gis590_sde]dude].sde"&lt;BR /&gt;tbl = "C:\ArcGIS Server Installation Files\\Connection to &lt;SPAN&gt;XXXXX&lt;/SPAN&gt;&lt;SPAN&gt;XXXXX&lt;/SPAN&gt;[gis590_sde]dude].sde\\gis590_sde.dude.tbl_documents"&lt;/P&gt;&lt;P&gt;edit= arcpy.da.Editor(GDBToEdit)&lt;BR /&gt;edit.startEditing(False, False)&lt;BR /&gt;edit.startOperation()&lt;BR /&gt;# formid to objectid&lt;BR /&gt;fieldList = ["formname", "sdsfeaturename", "doctitle", "creatorname", "approvername", "approvedate", "uploaddate", "filename", "mimetype", "filetype", "documentdata", "formid"]&lt;/P&gt;&lt;P&gt;valuesList = [formname, sdsfeaturename, doctitle, creatorname, approvername, approvedate, uploaddate, filename, mimetype, filetype, "",""]&lt;/P&gt;&lt;P&gt;cursor = arcpy.da.InsertCursor(tbl, fieldList)&lt;BR /&gt;try:&lt;BR /&gt; cursor.insertRow(valuesList)&lt;BR /&gt; del cursor&lt;BR /&gt;except:&lt;BR /&gt; traceback.print_exc()&lt;BR /&gt; del cursor&lt;/P&gt;&lt;P&gt;edit.stopOperation()&lt;BR /&gt;edit.stopEditing(True)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;Second Part&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#Create a match table &lt;BR /&gt;#matchtabletemplate = "C:\\ArcGIS Server Installation Files\\Connection to xxxxxx[gis590_sde]dude].sde\\gis590_sde.dude.tbl_matchtable"&lt;/P&gt;&lt;P&gt;matchtable = arcpy.CreateTable_management ("in_memory", "matchtable1")&lt;BR /&gt;arcpy.AddField_management(matchtable, "MATCHID", "LONG") &lt;BR /&gt;arcpy.AddField_management(matchtable, "FILENAME", "TEXT", field_length=100)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#Search cursor to get formid{MATCHID} for new record&lt;BR /&gt;whereclause = "formname = '{0}' AND sdsfeaturename = '{1}'".format(formname, sdsfeaturename) &lt;BR /&gt;fields = ["MATCHID", "FILENAME"]&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;with arcpy.da.SearchCursor(tbl, "formid", whereclause) as Scursor:&lt;BR /&gt; with arcpy.da.InsertCursor(matchtable, fields) as Icursor:&lt;BR /&gt; for row in Scursor:&lt;BR /&gt; Icursor.insertRow((row[0], filepath))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;intable = "C:\ArcGIS Server Installation Files\\Connection to XXXXX[gis590_sde]dude].sde\\gis590_sde.dude.tbl_documents"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;inmatchfield = "formid" #formid&lt;BR /&gt;#inmatchtable = "matchtable1"&lt;BR /&gt;matchjoinfield = "MATCHID"&lt;BR /&gt;matchfilefield = "FILENAME"&lt;BR /&gt;arcpy.AddAttachments_management(intable, inmatchfield, matchtable, matchjoinfield, matchfilefield)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 20 Mar 2017 17:40:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-gp-service-to-add-attachments/m-p/682098#M52824</guid>
      <dc:creator>EdwardParry1</dc:creator>
      <dc:date>2017-03-20T17:40:01Z</dc:date>
    </item>
  </channel>
</rss>

