Attribute field names

1187
4
06-10-2011 11:19 AM
VIKRANTKRISHNA
New Contributor III
Is there a way I can get just the field names of the layer. It would have made sense if layerInfo function could get attribute field names as well
0 Kudos
4 Replies
derekswingley1
Frequent Contributor
Feature Layers have a fields property.

What kind of layer are you using? You might need to use esri.request() to send a request to the layer's REST endpoint and get the field info. For instance, the response from this url:  http://sampleserver5.arcgisonline.com/ArcGIS/rest/services/Energy/Infrastructure/MapServer/7?f=json

contains info about all the fields for the layer.
0 Kudos
VIKRANTKRISHNA
New Contributor III
Hello,
      Unfortunately I have server 9.3.1, so I cannot use feature layer. Can you please tell me how to exactly use esri.request() in the javascript API, and how can I handle the response.

Thanks
0 Kudos
derekswingley1
Frequent Contributor
You can use a Feature Layer if you're using a 2.x version of the API. Here's a simple page that uses the 2.3 version of the API and a feature layer based on a 9.31 ArcGIS Server instance:
<!doctype html>
<html> 
  <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" /> 
  <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
      on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>FeatureLayer On Demand</title> 
    <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.3/js/dojo/dijit/themes/claro/claro.css"> 
    <style>
      html, body { height: 100%; width: 100%; margin: 0; padding: 0; }
    </style>
    <script type="text/javascript">djConfig = { parseOnLoad:true };</script> 
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.3"></script> 
    <script type="text/javascript"> 
      dojo.require("esri.map");
      dojo.require("esri.layers.FeatureLayer");

      var map;
      function init() {
        var extent = new esri.geometry.Extent({"xmin":-1091160.6279576253,"ymin":1464985.3783563056,"xmax":767849.8897356443,"ymax":2365380.130188807,"spatialReference":{"wkid":102039}});
        map = new esri.Map("map", { extent: extent });
        dojo.connect(map, "onLoad", initOperationalLayer);

        var base = new esri.layers.ArcGISDynamicMapServiceLayer("http://gis.apfo.usda.gov/ArcGIS/rest/services/CONUS/Overview/MapServer");
        map.addLayer(base);
      }
      function initOperationalLayer(map) {
        var content = "<b>Abbr</b>: ${STATE_ABBR}<br /><b>FIPS</b>: ${STATE_FIPS}";
        var infoTemplate = new esri.InfoTemplate("${STATE_NAME}", content);
        var featureLayer = new esri.layers.FeatureLayer("http://gis.apfo.usda.gov/ArcGIS/rest/services/CONUS/Overview/MapServer/1",{
          mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
          outFields: ["*"],
          infoTemplate: infoTemplate
        });
        // explicitly set OID field as the layer being used
        // does not have it's OID field visible
        featureLayer.objectIdField = 'STATE_FIPS';
        map.addLayer(featureLayer);
        map.infoWindow.resize(150,105);
      }
      dojo.ready(init);
    </script> 
  </head> 
  <body class="claro"> 
    <div style="position:relative;width:100%;height:100%;"> 
      <div id="map" style="border:1px solid #000;width:100%;height:100%;">
    </div> 
  </body> 
</html>



To use esri.request(), you would make a request to your map service layer endpoint and pull the info you need out of the fields array, here's an example:
// use esri.request to get field info
esri.request({
  url: 'http://gis.apfo.usda.gov/ArcGIS/rest/services/CONUS/Overview/MapServer/1',
  content: { f: 'json' },
  callbackParamName: 'callback',
  load: function(resp) { 
    dojo.forEach(resp.fields, function(field) {
      console.log('field name is ', field.name, ' and field alias is ', field.alias);
    });
  },
  error: function(err) { console.log('err: ', err); }
});
0 Kudos
VIKRANTKRISHNA
New Contributor III
Thanks Derek. Good to know that I can use feature layer with 9.31 also
0 Kudos