Solved! Go to Solution.
I am having a similar problem. My map has a DynamicMapServiceLayer (id="dynamicLayer") with two group layers - LayerA (id=0) and LayerB (id=4). LayerA has three subLayers (ids=1,2,3) and LayerB has three subLayers (ids=4,5,6). In my map layerA and subLayers 1 and 3 are visible and everything else is not so dynamicLayer's visibleLayers attribute equals [0,1,3].
Below is my pseudo code for printing the map:
var legendLayer = new esri.tasks.LegendLayer();
legendLayer.layerId = "dynamicLayer";
legendLayer.subLayerIds = [0,1,3];
var printTemplate = new esri.tasks.PrintTemplate();
printTemplate.format = "JPG";
printTemplate.layout = "Letter ANSI A Landscape";
var layoutOptions = {TitleText: "Test Map", scalebarUnit: 'Miles', legendLayers: [legendLayer]};
printTemplate.layoutOptions = layoutOptions;
var printParams = new esri.tasks.PrintParameters();
printParams.map = map;
printParams.outSpatialReference = map.spatialReference;
printParams.template = printTemplate;
var printURL = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export Web Map Task";
var printTask = new esri.tasks.PrintTask(printURL);
printTask.execute(printParams, function(result) {
window.open(result.url + "?timestamp="+(new Date().getTime()));
});
The problem is that the resulting map will include a legend with layers 1,2, and 3 in it even though they are not visible or in the legendLayer.subLayerIds. If I change the legendLayer.subLayerIds to [1,3] nothing in the legend shows up.