Adding ArcGISDynamicMapServiceLayer but working with FeatureLayers

Discussion created by esrijay on Jun 30, 2010
Latest reply on Jul 1, 2010 by esrijay
Maybe I missing the concept of FeatureLayers.....all I want to do is add a ArcGISDynamicMapServiceLayer (I want to add all the layers within the map/service in one shot) but I want to select a few of those layers and work with them as featureLayers. Is there a way to do this?

For example lets use the sample titled "feature layer with selection" example from the ArcGIS Resource Center - Javascript API. (Code is added below).

What if I want to add the entire Service not just the KSPetro layer.  In this case there is only one other layer: "Wells" but what if there was a bunch of layers in the service?  I don't want to add them all individually and I don't want them all to be FeatureLayers. 

Is there a way to add the entire service and set a few layers as FeatureLayers and work with them.   ""
Obviously, I don't want to add the entire service and then add the featurelayers, this will add some layers to the map twice.

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <meta http-equiv="X-UA-Compatible" content="IE=7" />
  <title>Layer in a map service - [ON-DEMAND]</title>
    <link rel="stylesheet" type="text/css" href="">
    <script type="text/javascript">djConfig = { parseOnLoad:true };</script>
    <script type="text/javascript" src=""></script>

    <script type="text/javascript">

      var selectionToolbar, featureLayer;

      function init() {
        var initialExtent = new esri.geometry.Extent(-97.5328,37.4344,-97.2582,37.64041, new esri.SpatialReference({wkid:4326}) );
        var map = new esri.Map("map", { extent: esri.geometry.geographicToWebMercator(initialExtent), slider: true, nav: true });
        dojo.connect(map, "onLoad", initSelectToolbar);
        var baseMapLayer = new esri.layers.ArcGISTiledMapServiceLayer("");

        var fieldsSelectionSymbol = new esri.symbol.SimpleFillSymbol().setColor(new dojo.Color([255,255,0,0.5]));
        fieldsSelectionSymbol.setOutline(new esri.symbol.SimpleLineSymbol("dashdot", new dojo.Color([255,0,0]), 2));
        var content = "<b>Status</b>: ${STATUS}" +
                      "<br><b>Cummulative Gas</b>: ${CUMM_GAS} MCF" +
                      "<br><b>Total Acres</b>: ${APPROXACRE}" +
                      "<br><b>Avg. Field Depth</b>: ${AVG_DEPTH} meters";
        var infoTemplate = new esri.InfoTemplate("${FIELD_NAME}", content);

        featureLayer = new esri.layers.FeatureLayer("",{
          mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
          infoTemplate: infoTemplate,
          outFields: ["*"]


        dojo.connect(featureLayer, "onSelectionComplete", sumGasProduction);

        dojo.connect(featureLayer, "onSelectionClear", function(features) {
          dojo.byId('messages').innerHTML = "<i>No Selected Fields</i>";

      function initSelectToolbar(map) {
        selectionToolbar = new esri.toolbars.Draw(map);
        var selectQuery = new esri.tasks.Query();
        dojo.connect(selectionToolbar, "onDrawEnd", function(geometry) {
          selectQuery.geometry = geometry;
          featureLayer.selectFeatures(selectQuery, esri.layers.FeatureLayer.SELECTION_NEW);

      function sumGasProduction(features) {
        var productionSum = 0;
        dojo.forEach(features, function(feature) {
          productionSum = productionSum + feature.attributes.CUMM_GAS;
        dojo.byId('messages').innerHTML = "<b>Selected Fields Production: " + productionSum + " mcf. </b>";



<body class="tundra">
  <button dojoType="dijit.form.Button" onClick="selectionToolbar.activate(esri.toolbars.Draw.EXTENT);">Select Fields</button>
  <button dojoType="dijit.form.Button" onClick="featureLayer.clearSelection();">Clear Selection</button><br>
  <div id="map" style="position: relative; width:700px; height:500px; border:1px solid #000;"></div>
  <span id="messages"></span>