pulldata @json to query Feature Service; Need help with security token

8338
15
01-07-2019 12:38 PM
AndrewHargreaves2
Occasional Contributor III

Hello Survey123 for ArcGIS‌ Team

I'm attempting to use the example given in the Custom pulldata() JavaScript Functions

here. However, I'm struggling with how to request a security token. Here's my setup:

  1. I query a Feature Service for a particular feature ID using the below:

    var url = "https://services1.arcgis.com/xxxxxxx/arcgis/rest/services/xxxxxx/FeatureServer/0/query?where="

    +"FACILITYID+%3D+%27" + ID +"%27"
    +"&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=0.0&units=esriSRUnit_Meter&returnGeodetic=false&outFields=*&returnHiddenFields=false&returnGeometry=true&multipatchOption=xyFootprint&maxAllowableOffset=&geometryPrecision=&outSR=&datumTransformation=&applyVCSProjection=false&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnDistinctValues=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&having=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&returnExceededLimitFeatures=true&quantizationParameters=&sqlFormat=none&f=pjson&token=BIG OLD SECURITY TOKEN"
  2. The above URL works great, and returns a JSON object for the desired feature that I can parse out and populate various fields within my Survey. However, I got that URL from experimenting with the Service URL query function. Because I was logged into to AGO when doing that I got a security token as part of the URL.

However, now I'm struggling with how to go about adding in a token request to my JS script. So:

  1. Is there a way to register this app in order to follow these steps?
  2. Or, is there a method to pull a users password (I know there's one for username)? 
  3. Or, the example given in the Custom pulldata() JavaScript Functions here includes the below. However, there's no further documentation on how I use the 'token' property to pull a token:

"Sometimes, particularly when using JS to access web services and secure ArcGIS services, you may need some properties that are not available through standard XLSForms. For this reason, we have also extended the collection of properties you can obtain through property() function as follows:

property('propertyname')

  • portalurl
  • token

..."

Any help would be greatly appreciated.

Jake Skinner

15 Replies
JakeSkinner
Esri Esteemed Contributor

You can make an HTTP request to generate the token:

var xhr = new XMLHttpRequest();
var tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken';
var params = 'f=pjson&username=jskinner_CountySandbox&password=******&referer=http://www.arcgis.com';
          
xhr.open('POST', tokenURL, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange  = function () {
   var response = (xhr.responseText);
   var obj = JSON.parse(response);
   var token = obj['token'];
      console.log(token);
   };
          
xhr.send(params);
0 Kudos
AndrewHargreaves2
Occasional Contributor III

Thanks Jake Skinner‌, but with a little help from James Tedrick‌ I figured this out. For anyone who'd like to replicate this I have attached my full survey, including the 'extension' folder containing the JS Script. Here's how to get it running:

  • Download and Unzip the attached
  • Publishing the Survey will create you a feature service that duplicates the schema of a Workforce assignments layer
  • Inside the 'extensions' folder open the QueryFS.js and insert the URL of the feature service that you'd like to query and pull attribute values from to insert into your Survey
  • Create a Workforce Project and in the dispatchers map ensure that the attribute used for the title of your popup is FacilityID (or some other unique value you use to query out individual features)
  • Within Workforce, under 'Advanced', ensure the Survey123 integration matches the below

  • Create a new assignment in Workforce. The Location should get automatically set to your FacilityID.
  • In the Workforce app open the project and "Survey at assignment". It should pass the FacilityID into the location field of your survey.
  • That same FacilityID gets passed to QueryFS.js which, along with a token, queries the Feature Service you define for that value and returns a JSON object.
  • There are examples in the excel of how to parse out the JSON to obtain the attribute values.
0 Kudos
KirkKirchner1
New Contributor II

Hi Jake,

Can I ask how where code would be implemented within the queryPolygon function seen on this esri blog post under "Working with web services"?

0 Kudos
DavidRunneals2
Occasional Contributor

This is AWESOME! Just got a project doing something similar to this I think that was going to use an external csv, but might just use a REST service since the script is pretty much already built.

I have a few questions after reading through this thread that I would like clarified:

1) Reading through the documentation though, I can't tell if it is still in beta or if it made it to the production application.

2) Is this functionality able to be used solely within Survey123 or is Workforce required? Looking to have a list of project numbers in a REST service that populate a dropdown within Survey123.

Thanks!

AndrewHargreaves2
Occasional Contributor III

I can answer #2 - it can be used with any rest service. You just need a mechanism to feed a value into a field in your survey, which the JS script takes to query "the other" feature service. In my case I passed Workforces' location into FacilityID. I then took the value of FacilityID (now populated) and used that in my JS query to "the other" feature service.

I can't answer #1 - I'd ask James Tedrick

0 Kudos
DavidRunneals2
Occasional Contributor

Are you using it in a beta version of Survey123 or production version?

Huh, you brought up a good use case that I never thought of!

Did you try creating a geopoint inside Survey123 and using that instead of your workflow of using Workforce to get the location? Which leads to my second question, do you have to pass it as a parameter IN to your survey so it runs on the loading of the survey, or is it able to be executed/ran once the survey is loaded?

0 Kudos
AndrewHargreaves2
Occasional Contributor III

I'm using #survey 123 connect‌ v3.1.113

For your first point - I create my own GeoPoint in Survey. The purpose of my workflow is to audit check the point collected in "the other" feature service. So I compare the location of the two points back in the office.

As for the second question - yes, it needs whatever value you intend to query your "other" feature service with passed in a parameter when opening the form. The JS script fires upon opening .

0 Kudos
JamesTedrick
Esri Esteemed Contributor

Hi David,

Beween the posting of the document and now, the property() function ws deprecated from the XLSForm specification; in it's place, you can use the pulldata('@property', 'token') calculation to retrieve the logged in user's token, and then pass that as a parameter.

DeonLengton
Esri Contributor

James Tedrick Is the JS functionality through the pulldata function available for all users on the Survey123 clients? We get "Extension disabled" errors when we try to perform this in Survey123... Is there a special beta or early adopter that we need to use to overcome this?

0 Kudos