Is it possible to use layer name instaed of layerid in identifyparams. However, over time, the layer order will change because I'll be adding new layers. Therefore, I want to refer to the layers by name, instead of id.can anyone suggest me please (am using arcgis api javascript api 3.17 example https://developers.arcgis.com/javascript/3/sandbox/sandbox.html?sample=find_popup)

2 Answers 2


Here's an example that returns the layerId of a named layer.

  var map, layerIds = [];

  ], function (
    Map, ArcGISDynamicMapServiceLayer, array) {

    map = new Map("mapDiv", {
      sliderOrientation : "horizontal"

    //Takes a URL to a non cached map service.
    var dynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer", {


    map.on("load", function () {
        layerIds.push(getLayerIdFromMap(dynamicMapServiceLayer, "states"));

    function getLayerIdFromMap(dynamicService, subLyrName) {
        var dLyr = map.getLayer(dynamicMapServiceLayer.id);
        var layerId;
        array.some(dLyr.layerInfos, function (lyrInfo) {
            if (lyrInfo.name === subLyrName) {
                layerId = lyrInfo.id;
        }, this);
        return layerId;
  • thank u kenbuja.. here is my code am not getting where should i apply the above code can u suggest me on that Aug 19, 2016 at 4:57
  • 1
    I have made edited the code to show how to create a layerIds array that you can use in the line identifyParams.layerIds = layerIds; in your mapReady function. You can run the line `layerIds.push(getLayerIdFromMap(dynamicMapServiceLayer, "your layer name"));' multiple times for each layerId you want to be included in the IdentifyTask
    – kenbuja
    Aug 19, 2016 at 13:50
], function (
  Map, InfoTemplate, ArcGISDynamicMapServiceLayer, SimpleFillSymbol,
  SimpleLineSymbol, IdentifyTask, IdentifyParameters, Popup,
  arrayUtils, Color, domConstruct
) {

    var identifyTask, identifyParams;

    var popup = new Popup({
        fillSymbol: new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
          new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
            new Color([255, 0, 0]), 2), new Color([255, 255, 0, 0.25]))
    }, domConstruct.create("div"));

    map.on("load", mapReady);

    var mapserviceURL = "";

    map.addLayer(new ArcGISDynamicMapServiceLayer(mapserviceURL));

    function mapReady() {
        map.on("click", executeIdentifyTask);
        //create identify tasks and setup parameters
        identifyTask = new IdentifyTask(mapserviceURL);

        identifyParams = new IdentifyParameters();
        identifyParams.tolerance = 3;
        identifyParams.returnGeometry = true;
        //identifyParams.layerIds = [35, 0];
        identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_ALL;
        identifyParams.width = map.width;
        identifyParams.height = map.height;

function executeIdentifyTask(event) {
        identifyParams.geometry = event.mapPoint;
        identifyParams.mapExtent = map.extent;

        var deferred = identifyTask
          .addCallback(function (response) {
              // response is an array of identify result objects
              // Let's return an array of features.
              return arrayUtils.map(response, function (result) {
                  var feature = result.feature;
                  var layerName = result.layerName;

                  feature.attributes.layerName = layerName;
                  if (layerName === 'Assembly') {
                      var taxParcelTemplate = new InfoTemplate("Assembly Details", "AssemblyCode:${AssemblyCode}<br /> AssemblyName:${AssemblyName}");
                  else if (layerName === 'District Hospital') {
                      var buildingFootprintTemplate = new InfoTemplate("District Hospital Details", "HealthFaci Name:${HealthFaci}<br /> CategoryNa:${CategoryNa}<br /> AssemblyCode :${AssemblyCode}");
                  return feature;

this is my code here am adding layerids maually can u suggest me whre ti apply the above code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.