I'm adding a Dynamic Map Service to an ArcGIS Server JavaScript API map, and I need to get hold of each component layer and its fields, so I can run a query.
See the sample ArcGIS Server services directory at http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/operations/MapServer
If you open this URL in a browser you can see the list of available layers (0,1,2) and each layer's fields.
How can I retrieve the list of fields programatically, within a JavaScript function? The code snippet below shows how to retrieve the layer endpoints, but I can't see how to get to the fields.
(One option is to add the layers as Feature Layers, but I'd prefer to avoid this if possible).
var url = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/HomelandSecurity/operations/MapServer/";
var dynLayer = new esri.layers.ArcGISDynamicMapServiceLayer(url);
map.addLayer(dynLayer);
dojo.connect(dynLayer, "onLoad", function() {
var infos = dynLayer.layerInfos;
for (var i = 0; i <= infos.length - 1; i++) {
var layerId = infos[i].id;
var restEndPoint = url + layerId;
//restEndPoint is the layer's URL - how can I retrieve its fields?
}
});
Thanks,
Steve (cross-posted to the ArcGIS Server forum)
Best Answer
Use esri.request() to hit each layer's REST endpoint to get info about fields. Here's a simple example:
That code uses v2.0 of the API but the same thing will work at 2.3 or 2.4. I originally posted in the Esri JS API forum.
Edit: Updated to handle all layers in a service. The code also now uses version 2.4 of the API.