[GIS] No legend showing at all in ArcGIS Server print task (MapExport)

arcgis-javascript-apiarcgis-server

I am using some stock print templates like the 2 in this sample. The problem is that the legend doesn't show up at all. I found a few forum questions showing problems with the legend but none of them involved having no legend at all. My code looks like this …

    var legendLayer = new LegendLayer();
    legendLayer.layerId = "Parcels_and_Zoning"; 
    legendLayer.subLayerIds = [4, 10];

The LegendLayer is included in the require section "esri/tasks/LegendLayer" and using typeof(legendLayer), it is an object. None of the samples that I have found show where to include space for the legend so maybe that is where things are going wrong. This next bit of my code is almost directly from the sample that I linked above …

        var layouts = [{
        name: "Letter ANSI A Landscape", 
        label: "Landscape (PDF)", 
        format: "pdf", 
        options: { 
          legendLayers: [legendLayer], // empty array means no legend
          scalebarUnit: "Miles",
          titleText: printTitle + ", Landscape PDF" 
        }
      }, {
        name: "Letter ANSI A Portrait", 
        label: "Portrait (Image)", 
        format: "jpg", // ETC. ETC. 

Note the comment saying "// empty array means no legend". I took the legendLayers option out entirely and STILL get no legend. Does anyone have any idea what I am doing wrong? I considered that I may need to add a legend in the html but I only need it for printing (I have not fully explored this). Do I need to create a legend in some way other than the code I've provided (the samples don't indicate this at all and all seem to avoid having a legend)? Is it possibly a problem with the service I have created ? The screenshot shows the result with no legend and sublayer 10 is the pink and amber colored polygon layer that I was hoping to get in the legend. screenshot

Best Answer

I spent hours trying to figure out the same issue. I was passing the LegendLayer objects with the layerId as it is defined in the arcgis MapServer service, and no legend showing at all. Finally, I realized that I had to pass the OPERATIONAL LAYERS, in those ids (Sniffing the JSON that it sends to ExportWebMap). So for example, it turned to be layerId = "layer1" and subLayerIds = [11, 21], and it generated perfectly the legend, with only the 11 and 21 layers. LegendLayer.layerId property stands for the assigned ID string of the layer in the map, not in the service.

Hope it had helped you everyone.

Related Question