Updated August 19, 2020. Added link to the API reference documentation
Updated August 6, 2020. Documented mode (edit, view) property. New in 3.10
The Survey123 web app JavaScript API is designed to help you embed, style and interact with Survey123 smart forms within your own web applications. At a glance, here is what you can do with this API and a little bit of JavaScript magic:
There are many applications for this new API. In the animation below, for example, a form is embedded within a web page used to report the speed of your internet connection. When the page is initially loaded, a big button is shown to help users initiate a quick internet speed test. One the connection speed is estimated, a Survey123 web form is loaded and the output of the speed test is automatically added into the form. At that point, the end user can complete the remaining questions and submit. The whole idea with this is that the logic to measure the internet speed of your connection sits in your web application, but when the time comes to submit data like your location or email contact, a Survey123 form is used for that. Through the Survey123 web app JavaScript API you can make your web app talk to the form and load the measured internet speed right away to make the process simpler.
Thanks to the Applications Prototype Lab for sharing the above example and making its source code available in GitHub!
You may also want to leverage the Web App JavaScript API to bring Survey123 forms into existing websites. Of course, you can always embed your survey by copying and pasting an embed link, but using the Web App API has the advantage that you can make your form interact with the website and vice-versa. For example, say you want to embed in the website of your city a form to capture incidents or issues that need attention. You can pre-populate certain questions in the form using information gathered from your own website. Similarly, you can feed your website with information entered by the user Survey123 while the form is being completed, or when the data is finally sent.
The following is about the simplest code snippet showing the basics of the Survey123 Web App JavaScript API. This sample loads a custom web form into a web page and pre-populates a couple of questions in it.
Lets have a closer look at the code:
There are a number of optional attributes you can also set. In the example above, we added the defaultQuestionValue attribute to set the default value of a couple of questions. Check the Web Form attributes section later in this document for a complete list.
To authorize the use of the Survey123 web app JavaScript API within your web application, you must provide a valid Client ID. You can create a Client ID through ArcGIS for Developers as follows:
ItemIDs uniquely identify ArcGIS items within your organization. You can find more information about itemIDs in the https://community.esri.com/community/gis/web-gis/arcgisonline/blog/2019/06/06/where-can-i-find-the-i... article. To get the ItemID of your web form:
Here is the list of all attributes supported by the Web Form object. The complete reference is at https://developers.arcgis.com/survey123/api-reference/web-app
Property Description
container | The HTML element that will contain the web form, typically a DIV. |
clientId | A valid Client ID for your domain registered in ArcGIS for Developers |
itemId | The ItemId of the web form you want to load. |
mode | (Optional) Defines if the form will create a new record on submit or update the specified record through the globalid property. Values are 'edit' or 'view'. If this property is not specified the submit button adds a new record. |
globalId | (Optional) Set this property only if mode='edit' or 'view'. Defines the record that should be loaded in the webform to be edited or viewed. For example: globalId: '72dc4442-773e-4490-88e0-fd7ad5c6d234'. |
portalUrl | (Optional) The URL of your ArcGIS portal. If left empty, defaults to https://www.arcgis.com |
token | Required if you work with private surveys. |
hideElements | (Optional) Elements can be set to be hidden, including navbar, theme, header, subHeader. For example: ["navbar","header","subheader"] You can also hide questions within your survey. For example: For example: ['field:yourQuestionName'] |
autoRefresh | (Optional) Enable auto refresh the survey after submit successfully. You can set autoRefresh=3 which will refresh the survey after 3 second |
defaultQuestionValue | (Optional) Lets you populate questions in the form. For example: {field_0: 'value1', field_1: 'value2'} |
isDisabledSubmitToFeatureService | (Optional) Set this property to true and the web form will not send data to your feature service. |
onFormLoaded | (Optional) Invokes a callback function when loaded. For example: function({form:Form, contentHeight: number }) |
onFormResized | (Optional) |
onFormSubmitted | (Optional) Invokes a callback function when the form is submitted. For example: function({surveyFeatureSet: SurveyFeatureSet}) |
onQuestionValueChanged | (Optional) Invokes a callback function when values in your form change. For example: function({field: string; value: any; path: string; repeatIndex: number; formId: string}) |
width | (Optional) Takes a number in pixels. Controls the width of the survey although anything smaller than 660 will be ignored. |
Web Form methods allow you to set the value of questions within your form from a JavaScript function. You can also use them to programmatically change the style of your form.
Method Description
.setQuestionValue | Used to set the value of a question, or questions, at run-time. For example:
webform.setQuestionValue({ |
.setGeopoint | Used to center the map and set the geopoint value to a given location. For example:
webform.setGeopoint({ x=-117.34, y=43.78 });
It is commonly used in combination with the Geolocation API. |
.setStyle | Used to apply CSS styling to your web form. For example:
webform.setStyle({ |
In developer samples and throughout the blogs and documentation you will often see that the API is imported using this URL: http://survey123.arcgis.com/api/jsapi This is the home of the API and always returns the latest available version of it (non-version locked).
If you rather lock the version of the API used in your web application to a specific version, all you need to do is to add the version number you want at the end of the URL. For example: http://survey123.arcgis.com/api/jsapi/3.13 will give you access to the 3.13 version specifically.
You may want to consider the use of version-locked imports in your code to avoid surprises when new releases are made available.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.