Select to view content in your preferred language

Update CSV linked to Surveys via script

453
6
Jump to solution
08-19-2024 12:16 PM
DuncanC
Frequent Contributor

I want to automate the updating of the CSV files my surveys use.  But I cannot find a way to do the equivalent of pushing update data button in the online portal via script.  Using either the REST interface directly or the python SDK I can download the CSV, but I can't find a method to replace it with new data. 

I could delete it, and upload a new copy, but that will have a new ID and I'm pretty certain it's not going to be linked to my surveys.  I've looked into adding a link to the new ID as linked content on the surveys via python, but that also looks to be a dead end and not available.  

Hoping I'm just missing something and there is a way to do this.  Any advice would be greatly appreciated.

0 Kudos
1 Solution

Accepted Solutions
AmeWunderle
Regular Contributor

Hi Duncan,

I run a daily python script  to update a csv that I reference in one of my surveys in Survey123. I also only use AGOL not enterprise.

The steps I follow are:

1. re-create the csv locally exactly how it is in AGOL so that you can use the update method on the csv in AGOL

2. run the following script 

import datetime
from arcgis import GIS
from arcgis import features
from arcgis.features import FeatureLayerCollection
import datetime
import sys
 
# Connect to the GIS
gis =  GIS(profile="") # Your profile name
token = gis._con.token
 
# set variables and access json
csvID = ""  # the id number of your csv
csv_item_title = "" # the title of your csv
localfile = "" # the local path to your file you wish to replace the csv with
 
currentdate = datetime.date.today()
 
# set log file for data logging
fp = open("","a") # set up a log file as this is never a bad idea
 
# Query to see if item exists
print ("...Searching for feature service...")
the_services = gis.content.search(query = 'title:' + csv_item_title + " AND " + 'id:' + csvID)
if len(the_services)!=1:
    print("Couldn't find feature service" + csv_item_title+ " in AGOL. Exiting script")
    error_message = currentdate.strftime("%Y/%m/%d")+','+'csv not found\n'
    fp.write(error_message)
    fp.close()
    sys.exit()
print ("....found feature service " + csv_item_title+ " in AGOL")
 
#Update CSV Table in AGOL from local CSV file
fs = gis.content.get(csvID)
try:
    fs.update(data=localfile)
    print (".....updating csv file in AGOL")
except:
    error_message = currentdate.strftime("%Y/%m/%d")+','+'update in AGOL not successful\n'
    print ("..error in updating csv file. Exiting script")
    fp.write(error_message)
    fp.close()
    sys.exit()

# write the success to the log file
message = currentdate.strftime("%Y/%m/%d")+','+'Yes\n'
fp.write(message)
fp.close()
print("Script successful")
 

View solution in original post

6 Replies
HannesVogel
Frequent Contributor

Hi @DuncanC 

my colleague is using FME Flow - maybe here is a good starting point: https://community.esri.com/t5/arcgis-data-interoperability-blog/how-fme-flow-or-form-can-write-to-ar...

0 Kudos
DuncanC
Frequent Contributor

Thanks for the suggestion. 

I should have mentioned I'm using AGOL not a enterprise install so I don't think that solution works.

0 Kudos
HannesVogel
Frequent Contributor

if you have the FME possibility there is an AGOL connector as well: https://support.safe.com/hc/en-us/articles/25407563623565-How-to-use-the-EsriArcGISOnlineConnector

0 Kudos
DuncanC
Frequent Contributor

I'm not interested in the FME part, but if their connector can do what it claims it give me hope that what I want is indeed possible, I just need to find the correct REST endpoint.

AmeWunderle
Regular Contributor

Hi Duncan,

I run a daily python script  to update a csv that I reference in one of my surveys in Survey123. I also only use AGOL not enterprise.

The steps I follow are:

1. re-create the csv locally exactly how it is in AGOL so that you can use the update method on the csv in AGOL

2. run the following script 

import datetime
from arcgis import GIS
from arcgis import features
from arcgis.features import FeatureLayerCollection
import datetime
import sys
 
# Connect to the GIS
gis =  GIS(profile="") # Your profile name
token = gis._con.token
 
# set variables and access json
csvID = ""  # the id number of your csv
csv_item_title = "" # the title of your csv
localfile = "" # the local path to your file you wish to replace the csv with
 
currentdate = datetime.date.today()
 
# set log file for data logging
fp = open("","a") # set up a log file as this is never a bad idea
 
# Query to see if item exists
print ("...Searching for feature service...")
the_services = gis.content.search(query = 'title:' + csv_item_title + " AND " + 'id:' + csvID)
if len(the_services)!=1:
    print("Couldn't find feature service" + csv_item_title+ " in AGOL. Exiting script")
    error_message = currentdate.strftime("%Y/%m/%d")+','+'csv not found\n'
    fp.write(error_message)
    fp.close()
    sys.exit()
print ("....found feature service " + csv_item_title+ " in AGOL")
 
#Update CSV Table in AGOL from local CSV file
fs = gis.content.get(csvID)
try:
    fs.update(data=localfile)
    print (".....updating csv file in AGOL")
except:
    error_message = currentdate.strftime("%Y/%m/%d")+','+'update in AGOL not successful\n'
    print ("..error in updating csv file. Exiting script")
    fp.write(error_message)
    fp.close()
    sys.exit()

# write the success to the log file
message = currentdate.strftime("%Y/%m/%d")+','+'Yes\n'
fp.write(message)
fp.close()
print("Script successful")
 
DuncanC
Frequent Contributor

Thank you very much!!  Not sure how I missed that earlier today but your script helped me find the update method I need. 

https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.Item.update

0 Kudos