Generate Survey123 Report

8886
35
Jump to solution
11-28-2018 09:56 AM
ChelseaRozek
MVP Regular Contributor

Really excited about the latest Python API update. Would like to be able to export survey responses as reports with Python. I'm currently stuck at this part. I can see that the reports get generated and I can download them (image below from Survey123 website), but I receive an error in the code: KeyError: 'results' (see attachment). I'm also curious if you can access the generated reports directly with Python after making them.

0 Kudos
35 Replies
ChrisBerryman
New Contributor III

Yeah that is what I tried.  here is what i have. 

ChrisBerryman_0-1628175041697.png

 

Notice I also try passing in additional properties that are noted in the api documentation yet I get an error.  I have also noticed that when only passing in the where parameter and the report name, I get a zip file with docx files but they only have the default name which is not helpful.  have you seen this before?

0 Kudos
BHeist
by
New Contributor III

Chris, 

I had not seen that before since I've only ever worked with 4 positional arguments at one time. The only thing that was holding mine up previously was the structure of the SQL statement for the 'where_filter'. Once I was able to get that sorted out then it worked fine for me.  

 

What did help get that sorted out was going to the 'Service URL' for the service layer I was querying for the feature report generation and using the 'Validate SQL' operation against that service. That helped me make sure my SQL statement was valid and also gave me a clue as to why it might not be working. 

 

So maybe start there? It's always good to rule things out one at a time so you know what's holding you up. 

 

Hope that helps! 

 

 

Here's a code sample from my working script that I ran through a Notebook in Pro

 

from arcgis.gis import GIS
from IPython.display import display
gis = GIS("pro")

from arcgis.apps.survey123._survey import SurveyManager, Survey
survey_mgr = SurveyManager(gis)

 

## Set / change all variables here

output_folder = "C:\\Users\\5018beh\\Downloads"   # location on your local machine

survey = survey_mgr.get("insert ID here")    # Creating survey object using survey ID

surveyID = ("insert ID here")    # surveyID

template = survey.report_templates[2]   # Feature report template from Portal

utc_offset = '+0400'    # UTC offset for central time

report_title = "NCLF Daily Reports"    # Title that will show in S123 recent task list

where_filter = "last_edited_date >= CURRENT_TIMESTAMP - INTERVAL '7' DAY"   # Filter for survey submissions in the last 7 days

 

# Generate Survey Feature Report in Survey123
survey.generate_report(template, where_filter, utc_offset, report_title)

rachelm
New Contributor III

When I run a where statement that matches a single feature, I can access and download the resulting Word doc.

But I have two issues:

  1. When I run a where statement that matches multiple features or where is  "1=1" then I get a resulting Zip file item with a type of "Code Sample" in Agol. Python is trying to find a report, but the Code Sample type is not a Word Doc, and is skipped over so I can't download it.
  2. I can see how the task in the Survey123 page is named correctly, but the output files themselves have different names, with part of the item id. similar problem as @ChrisBerryman

It seems other people ARE able to download the zip file, or at least the multiple reports?

0 Kudos
BHeist
by
New Contributor III

rachelm,

Try using the following code snippet. It will look through the content of the user and download anything that matched the surveyID of the report that was just generated.  You'll need to define the variables 'surveyID' and 'output_folder' to match your environment. 

 

# Search for generated survey123 reports owned by the logged-in user
my_content = gis.content.search(query="owner:" + gis.users.me.username, item_type="Code Sample", max_items=15)


for x in my_content:
if surveyID in x.description:

# Get ID of each Word doc AGOL item
id = x.id

# Get item with matching ID
data_item = gis.content.get(id)

# Download each Word doc to specified location
data_item.download(output_folder)

# Delete item from My Content after download as it's no longer needed
data_item.delete()


rachelm
New Contributor III

@BHeist  that did work, thanks!

Any idea how I can output the name of the saved item in AGOL to be similar to the default you can generate from the Survey123 page itself (e.g. FormName_OID_Date)? I'm stuck with nonsense names with part of the item id.

0 Kudos
BHeist
by
New Contributor III

@rachelm 

I'm not sure about that one actually. Maybe check the API reference? One of those arguments might do the trick!

https://developers.arcgis.com/python/api-reference/arcgis.apps.survey123.html

 

0 Kudos