Select to view content in your preferred language

Getting feature layer attributes as object without Query using query class

4449
9
Jump to solution
02-13-2017 06:05 AM
by Anonymous User
Not applicable

I know I can get feature layer attributes using Query class, but is there another way to get all attributes in a feature layer?

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Ah. that make sense now. Here is a 4.2 version of the sample:

<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />
  <title>Request Test</title>
  <link rel="stylesheet" href="http://js.arcgis.com/4.2/esri/css/main.css">
  <link rel="stylesheet" type="text/css" href="http://js.arcgis.com/4.2/dijit/themes/claro/claro.css">
  <style>
    html,
    body,
    #viewDiv {
      height: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
    }
    body {
      background-color: #FFF;
      overflow: hidden;
      font-family: "Trebuchet MS";
    }
    #header {
      height: 3%;
      overflow: auto;
    }
  </style>
  <script>
    var dojoConfig = {
      parseOnLoad: true
    };
  </script>
  <script src="http://js.arcgis.com/4.2/"></script>
  <script>
    var map;
    require([
      "esri/Map",
      "esri/views/MapView",
      "dojo/_base/lang",
      "esri/request",
      "dijit/layout/BorderContainer",
      "dijit/layout/ContentPane",
      "dojo/domReady!"
    ], function(
      Map, MapView, lang, esriRequest
    ) {
      map = new Map({
        basemap: "topo"
      });
      
      var view = new MapView({
        container: "viewDiv",
        map: map,
        zoom: 4,
        center: [15, 65]
      });
      var url = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/NapervilleShelters/FeatureServer/0/query?...";
      esriRequest(url,{
          responseType:'json',
          callbackParamName:'callback',
          timeout:15000
        }).then(lang.hitch(this,function(resp){
          console.info(resp.data);
          alert("Data returned");
        }),lang.hitch(this,function(error){
          alert("Data failed");
        }));
    });
  </script>
</head>

<body class="claro">
  <div id="viewDiv" data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline', gutters:false" style="padding:0px;margin:0px;">
    <div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'left'" style="width:100%;height:100%;border:none;padding:0px;margin:0px;"></div>
  </div>
</body>

</html>

View solution in original post

0 Kudos
9 Replies
SarojThapa1
Frequent Contributor

Makiko,

Do you want to display attributes of a feature layer as a Popup Window? If yes, then check this sample out.

ArcGIS API for JavaScript Sandbox 

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Makiko,

   Hmm.. any particular reason why? The QueryTask is the main JS API route to get attribute values. The other way would be to just use a esriRequest directly to the map service to get the attribute values for a know ObjectID.

0 Kudos
by Anonymous User
Not applicable

Hi Saroj and Robert,

Thank you so much for your quick response.  There is no special reason not using QueryTask class.  Since I need all attributes in a feature layer in JSON format so I thought it might have something other than QueryTask class that gives me direct access to JSON formatted attributes.

esriRequest seems to be the answer for my question, but does it work on a feature service layer?  I looked at the example and replaced url to feature service layer, but it didn't return anything.  Does esriRequest only work for GeoJson layer?

var url = "https://services.arcgis.com/.../arcgis/rest/services/.../FeatureServer/0"

esriRequest(url, {
responseType: "json"
}).then(function(response){
// The requested data
console.log(response);
var dataJson = response.data;
console.log(dataJson)
});

Thank you,

Makiko

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Makiko,

   You absolutely can use an esriRequest on things other then GeoJSON.

esriRequest({
  url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NapervilleShelters/FeatureServer/0/query...",
  handleAs: "json",
  timeout: 10000
  }
).then(function(response){
  console.info(response);
});
by Anonymous User
Not applicable

Hi Robert,

I tested your code, but it gave me an error.  This is not a big deal so I fixed it and tried it again.  But it still doesn't give me any response.  Though, the example code that uses GeoJson works fine. I wonder what is the problem with the feature service layer.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Makiko,

   I am not sure whats going wrong in your code then. Here is a full example of using esriRequest:

<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />
  <title>Request Test</title>
  <link rel="stylesheet" type="text/css" href="http://js.arcgis.com/3.18/dijit/themes/claro/claro.css">
  <link rel="stylesheet" type="text/css" href="http://js.arcgis.com/3.18/esri/css/esri.css">
  <style>
    html,
    body,
    #mainWindow {
      height: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
    }
    body {
      background-color: #FFF;
      overflow: hidden;
      font-family: "Trebuchet MS";
    }
    #header {
      height: 3%;
      overflow: auto;
    }
  </style>
  <script>
    var dojoConfig = {
      parseOnLoad: true
    };
  </script>
  <script src="http://js.arcgis.com/3.18/"></script>
  <script>
    var map;
    require([
      "esri/map",
      "dojo/_base/lang",
      "esri/request",
      "dijit/layout/BorderContainer",
      "dijit/layout/ContentPane",
      "dojo/domReady!"
    ], function(
      Map, lang, esriRequest
    ) {
      map = new Map("map", {
        basemap: "topo",
        center: [-120.1883, 37.0868],
        zoom: 6
      });

      esriRequest({
          url:"http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Louisville/LOJIC_LandRecords_Louisville/M...",
          content:{
            f:'json'
          },
          handleAs:'json',
          callbackParamName:'callback',
          timeout:15000
        }).then(lang.hitch(this,function(resp){
          console.info(resp);
          if(resp.layers){
            resp.layers.forEach(function(layerInfo){
               //Do something
            });
          }
        }),lang.hitch(this,function(error){
          alert("Data failed");
        }));
    });
  </script>
</head>

<body class="claro">
  <div id="mainWindow" data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline', gutters:false" style="padding:0px;margin:0px;">
    <div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'left'" style="width:100%;height:100%;border:none;padding:0px;margin:0px;"></div>
  </div>
</body>

</html>
0 Kudos
by Anonymous User
Not applicable

Hi Robert,

I am using JavaScript API 4.2.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Ah. that make sense now. Here is a 4.2 version of the sample:

<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />
  <title>Request Test</title>
  <link rel="stylesheet" href="http://js.arcgis.com/4.2/esri/css/main.css">
  <link rel="stylesheet" type="text/css" href="http://js.arcgis.com/4.2/dijit/themes/claro/claro.css">
  <style>
    html,
    body,
    #viewDiv {
      height: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
    }
    body {
      background-color: #FFF;
      overflow: hidden;
      font-family: "Trebuchet MS";
    }
    #header {
      height: 3%;
      overflow: auto;
    }
  </style>
  <script>
    var dojoConfig = {
      parseOnLoad: true
    };
  </script>
  <script src="http://js.arcgis.com/4.2/"></script>
  <script>
    var map;
    require([
      "esri/Map",
      "esri/views/MapView",
      "dojo/_base/lang",
      "esri/request",
      "dijit/layout/BorderContainer",
      "dijit/layout/ContentPane",
      "dojo/domReady!"
    ], function(
      Map, MapView, lang, esriRequest
    ) {
      map = new Map({
        basemap: "topo"
      });
      
      var view = new MapView({
        container: "viewDiv",
        map: map,
        zoom: 4,
        center: [15, 65]
      });
      var url = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/NapervilleShelters/FeatureServer/0/query?...";
      esriRequest(url,{
          responseType:'json',
          callbackParamName:'callback',
          timeout:15000
        }).then(lang.hitch(this,function(resp){
          console.info(resp.data);
          alert("Data returned");
        }),lang.hitch(this,function(error){
          alert("Data failed");
        }));
    });
  </script>
</head>

<body class="claro">
  <div id="viewDiv" data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline', gutters:false" style="padding:0px;margin:0px;">
    <div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'left'" style="width:100%;height:100%;border:none;padding:0px;margin:0px;"></div>
  </div>
</body>

</html>
0 Kudos
by Anonymous User
Not applicable

Hi Robert,

Wonderful!! It works perfectly.  Thank you so much!

0 Kudos