<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Setting legendLayer subLayerIds for PrintTask with Multiple Map Services in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/setting-legendlayer-sublayerids-for-printtask-with/m-p/155675#M14493</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have successfully gotten the PrintTask to work with multiple map services with a legend. &amp;nbsp;By hard-coding the list of legendLayer.subLayerIds, it works.&lt;/P&gt;&lt;P&gt;However, I want to look up the visible layers in the map and return an array of layer indexes and assign that array to the legendLayer.subLayerIds for each map service. &amp;nbsp;I have tried various things such as (1.) using a loop and pushing each array value into legendLayer.subLayerIds, (2.) using brackets (e.g., legendLayer.subLayerIds = [returnedArray], (3.) creating a string instead of an array with commas in between each layer index (e.g., "[38,44]"). &amp;nbsp;Option (3.) actually works, but even if I pass a blank string (e.g., "[ ]"), all of the visible layers for that map service are included in the print legend.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is the code that works with the hard-coded values for the legendLayer.subLayerIds:&lt;/P&gt;&lt;P&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;BR /&gt;&amp;lt;html&amp;gt;&lt;BR /&gt;&amp;lt;head&amp;gt;&lt;BR /&gt; &amp;lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&amp;gt;&lt;BR /&gt; &amp;lt;meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;meta name="apple-mobile-web-app-capable" content="yes"&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;lt;meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;lt;meta name="mobile-web-app-capable" content="yes"&amp;gt;&lt;BR /&gt; &amp;lt;title&amp;gt;ArcGIS dynamic and tile layers using Popup and InfoTemplates&amp;lt;/title&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;lt;script type="text/javascript" src="../jquery/jquery-1.11.3.min.js"&amp;gt;&amp;lt;/script&amp;gt;&lt;BR /&gt; &amp;lt;!--&amp;lt;script type="text/javascript" src="../jquery-ui-1.10.4/jquery-ui-1.10.4/ui/jquery-ui.js"&amp;gt;&amp;lt;/script&amp;gt;--&amp;gt;&lt;BR /&gt; &amp;lt;script type="text/javascript" src="../jquery/jquery-ui-1.10.4/ui/jquery-ui.js"&amp;gt;&amp;lt;/script&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;lt;link rel="stylesheet" href="&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fjs.arcgis.com%2F3.20%2Fesri%2Fcss%2Fesri.css" rel="nofollow" target="_blank"&gt;https://js.arcgis.com/3.20/esri/css/esri.css&lt;/A&gt;&lt;SPAN&gt;"&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt; &amp;lt;style&amp;gt;&lt;BR /&gt; html, body, #ui-esri-map {&lt;BR /&gt; width: 100%;&lt;BR /&gt; height: 100%;&lt;BR /&gt; margin: 0;&lt;BR /&gt; padding: 0;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; .esriPopup .titlePane {&lt;BR /&gt; text-shadow: none;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;.esriPopup .titleButton.next {&lt;BR /&gt; right: 40px;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;.esriPopup .titleButton.prev {&lt;BR /&gt; right: 53px;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;.demographicInfoContent {&lt;BR /&gt; padding-top: 10px;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;.demographicInnerSpacing {&lt;BR /&gt; display: inline-block;&lt;BR /&gt; width: 8px;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;.demographicNumericPadding {&lt;BR /&gt; width: 90px;&lt;BR /&gt; display: inline-block;&lt;BR /&gt; text-align: right;&lt;BR /&gt; }&lt;BR /&gt; #info{&lt;BR /&gt; top: 0px;&lt;BR /&gt; right: 0px;&lt;BR /&gt; position: absolute;&lt;BR /&gt; z-index: 99;&lt;BR /&gt; opacity: 0.9;&lt;BR /&gt; background-color: whitesmoke;&lt;BR /&gt; border-bottom-left-radius: 8px;&lt;BR /&gt; padding: 0px 0px 0px 10px;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;#infoPrint{&lt;BR /&gt; bottom: 5px;&lt;BR /&gt; left: 5px;&lt;BR /&gt; position: absolute;&lt;BR /&gt; z-index: 99;&lt;BR /&gt; opacity: 0.9;&lt;BR /&gt; background-color: lightpink;&lt;BR /&gt; border-top-right-radius: 8px;&lt;BR /&gt; padding: 10px 10px 20px 10px;&lt;BR /&gt; width: 20%;&lt;BR /&gt; }&lt;BR /&gt; &amp;lt;/style&amp;gt;&lt;BR /&gt;&lt;SPAN&gt; &amp;lt;script src="&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fjs.arcgis.com%2F3.20%2F" rel="nofollow" target="_blank"&gt;https://js.arcgis.com/3.20/&lt;/A&gt;&lt;SPAN&gt;"&amp;gt;&amp;lt;/script&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt; &amp;lt;script&amp;gt;&lt;BR /&gt; $(document).ready(function () {&lt;BR /&gt; var map;&lt;BR /&gt; // layer indexes in the map services&lt;BR /&gt; var indexPoint_StormwaterOuterZoom = 34;&lt;BR /&gt; var indexPoint_StormwaterMiddleZoom = 35;&lt;BR /&gt; var indexPoint_StormwaterCloseUpZoom = 36;&lt;BR /&gt; var indexPoint_ControlPts = 1;&lt;BR /&gt; var indexPoint_FiberPts = 9;&lt;BR /&gt; var indexPoint_ReferencePts = 11;&lt;BR /&gt; var indexPoint_SchoolBusStopsOSPI = 19;&lt;BR /&gt; var indexPoint_AvistaPoleStructure = 28;&lt;BR /&gt; var indexPoint_Shop = 5;&lt;BR /&gt; var indexLine_PriorityArray = 13;&lt;BR /&gt; var indexLine_RoadSegments = 38;&lt;BR /&gt; var indexLine_Railroad = 41;&lt;BR /&gt; var indexLine_Waterline = 44;&lt;BR /&gt; var indexPoly_NoTrucks = 32;&lt;BR /&gt; var indexPoly_CountyBdry = 13;&lt;BR /&gt; var indexPoly_Parcels = 53;&lt;BR /&gt; var indexPoly_MuniBdry = 83;&lt;BR /&gt; var indexPoly_Waterbody = 27;&lt;BR /&gt; require([&lt;BR /&gt; "dojo/dom-construct",&lt;BR /&gt; "esri/Color",&lt;BR /&gt; "esri/dijit/Popup",&lt;BR /&gt; "esri/InfoTemplate",&lt;BR /&gt; "esri/dijit/Legend",&lt;BR /&gt; "esri/layers/ArcGISDynamicMapServiceLayer",&lt;BR /&gt; "esri/map",&lt;BR /&gt; "esri/tasks/PrintTask",&lt;BR /&gt; "esri/tasks/PrintTemplate",&lt;BR /&gt; "esri/tasks/PrintParameters",&lt;BR /&gt; "esri/tasks/LegendLayer",&lt;BR /&gt; "esri/config",&lt;BR /&gt; "dojo/parser",&lt;BR /&gt; "dojo/domReady!"&lt;BR /&gt; ], function (&lt;BR /&gt; domConstruct, Color, Popup, InfoTemplate, Legend,&lt;BR /&gt; ArcGISDynamicMapServiceLayer, Map,&lt;BR /&gt; PrintTask, PrintTemplate, PrintParameters, LegendLayer,&lt;BR /&gt; esriConfig,&lt;BR /&gt; parser&lt;BR /&gt; ) {&lt;/P&gt;&lt;P&gt;parser.parse();&lt;BR /&gt; //This sample may require a proxy page to handle communications with the ArcGIS Server services. You will need to&lt;BR /&gt; //replace the url below with the location of a proxy on your machine. See the 'Using the proxy page' help topic&lt;BR /&gt; //for details on setting up a proxy page.&lt;BR /&gt; esriConfig.defaults.io.proxyUrl = "/proxy/";&lt;BR /&gt; esriConfig.defaults.io.alwaysUseProxy = false;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; var initialExtent = new esri.geometry.Extent(&lt;BR /&gt; -117.83, 47.23,&lt;BR /&gt; -117.03, 48.055,&lt;BR /&gt; new esri.SpatialReference({ wkid: 4326 })&lt;BR /&gt; );&lt;/P&gt;&lt;P&gt;map = new Map("ui-esri-map", {&lt;BR /&gt; basemap: "topo",&lt;BR /&gt; extent: initialExtent,&lt;BR /&gt; sliderStyle: "small"&lt;BR /&gt; });&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;var polygonsLayer = new ArcGISDynamicMapServiceLayer("&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Figis.spokanecounty.org%2Farcgis%2Frest%2Fservices%2FPublicWorks%2FPublicWorksQueriesPolygons%2FMapServer" rel="nofollow" target="_blank"&gt;http://igis.spokanecounty.org/arcgis/rest/services/PublicWorks/PublicWorksQueriesPolygons/MapServer&lt;/A&gt;&lt;SPAN&gt;", {&lt;/SPAN&gt;&lt;BR /&gt; "id": "polygonsLayer",&lt;BR /&gt; "opacity": 0.75&lt;BR /&gt; });&lt;/P&gt;&lt;P&gt;polygonsLayer.setVisibleLayers([53]);&lt;BR /&gt; map.addLayer(polygonsLayer);&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;SPAN&gt; var linesLayer = new ArcGISDynamicMapServiceLayer("&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Figis.spokanecounty.org%2Farcgis%2Frest%2Fservices%2FPublicWorks%2FPublicWorksQueriesLines%2FMapServer" rel="nofollow" target="_blank"&gt;http://igis.spokanecounty.org/arcgis/rest/services/PublicWorks/PublicWorksQueriesLines/MapServer&lt;/A&gt;&lt;SPAN&gt;", {&lt;/SPAN&gt;&lt;BR /&gt; "id": "linesLayer",&lt;BR /&gt; "opacity": 0.75&lt;BR /&gt; });&lt;/P&gt;&lt;P&gt;linesLayer.setVisibleLayers([38]);&lt;BR /&gt; map.addLayer(linesLayer);&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;var pointsLayerURL = "&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Figis.spokanecounty.org%2Farcgis%2Frest%2Fservices%2FPublicWorks%2FPublicWorksQueriesPoints%2FMapServer" rel="nofollow" target="_blank"&gt;http://igis.spokanecounty.org/arcgis/rest/services/PublicWorks/PublicWorksQueriesPoints/MapServer&lt;/A&gt;&lt;SPAN&gt;";&lt;/SPAN&gt;&lt;BR /&gt; var pointsLayerOptions = {&lt;BR /&gt; "id": "pointsLayer",&lt;BR /&gt; "opacity": 0.8,&lt;BR /&gt; "showAttribution": false&lt;BR /&gt; };&lt;BR /&gt; var pointsLayer = new ArcGISDynamicMapServiceLayer(pointsLayerURL, pointsLayerOptions);&lt;/P&gt;&lt;P&gt;pointsLayer.setVisibleLayers([4, 5]);&lt;BR /&gt; map.addLayer(pointsLayer);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;map.on("load", function (evt) {&lt;BR /&gt; var legend = new Legend({&lt;BR /&gt; map: map,&lt;BR /&gt; layerInfos: [{&lt;BR /&gt; layer: linesLayer,&lt;BR /&gt; title: "Lines Layer"&lt;BR /&gt; },&lt;BR /&gt; {&lt;BR /&gt; layer: pointsLayer,&lt;BR /&gt; hideLayers: [4],&lt;BR /&gt; title: "Points Layer"&lt;BR /&gt; }]&lt;BR /&gt; }, "legendDiv");&lt;/P&gt;&lt;P&gt;legend.startup();&lt;/P&gt;&lt;P&gt;firstPrint();&lt;BR /&gt; });&lt;/P&gt;&lt;P&gt;var customHeader = 'Spokane County Public Works';&lt;BR /&gt; var customScale;&lt;BR /&gt; var printToScale;&lt;/P&gt;&lt;P&gt;// ***************************************************************&lt;BR /&gt; // btnPrint click function&lt;BR /&gt; // ***************************************************************&lt;BR /&gt; $('#btnPrint').on('click', function () {&lt;BR /&gt; $(document.body).css({ 'cursor': 'wait' });&lt;BR /&gt; map.setMapCursor("wait");&lt;BR /&gt; if ($('#txtCustomHeader').val().length &amp;lt; 1) {&lt;BR /&gt; customHeader = 'Spokane County Public Works'&lt;BR /&gt; }&lt;BR /&gt; else {&lt;BR /&gt; customHeader = $('#txtCustomHeader').val();&lt;BR /&gt; }&lt;BR /&gt; if ($('#chkPrintScale').is(':checked')) {&lt;BR /&gt; printToScale = true;&lt;BR /&gt; }&lt;BR /&gt; else {&lt;BR /&gt; printToScale = false;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;if ($('#txtCustomScale').val().length &amp;lt; 1) {&lt;BR /&gt; customScale = map.getScale();&lt;BR /&gt; }&lt;BR /&gt; else {&lt;BR /&gt; customScale = $('#txtCustomScale').val();&lt;BR /&gt; }&lt;BR /&gt; // points legend&lt;BR /&gt; var legendLayerPrintPoints = new LegendLayer();&lt;BR /&gt; legendLayerPrintPoints.layerId = "pointsLayer";&lt;BR /&gt; legendLayerPrintPoints.subLayerIds = [indexPoint_Shop];&lt;BR /&gt; // lines legend&lt;BR /&gt; var legendLayerPrintLines = new LegendLayer();&lt;BR /&gt; legendLayerPrintLines.layerId = "linesLayer";&lt;BR /&gt; legendLayerPrintLines.subLayerIds = [indexLine_RoadSegments];&lt;BR /&gt; // polygons legend&lt;BR /&gt; var legendLayerPrintPolygons = new LegendLayer();&lt;BR /&gt; legendLayerPrintPolygons.layerId = "polygonsLayer";&lt;BR /&gt; legendLayerPrintPolygons.subLayerIds = [indexPoly_CountyBdry, indexPoly_Parcels, indexPoly_MuniBdry];&lt;/P&gt;&lt;P&gt;var visiblePrintSublayerPoints = [], visiblePrintSublayerLines = [], visiblePrintSublayerPolygons = [];&lt;BR /&gt; // No need to exclude any Point layers from the legend&lt;BR /&gt; if (legendLayerPrintPoints.subLayerIds.length &amp;gt; 0) {&lt;BR /&gt; visiblePrintSublayerPoints = legendLayerPrintPoints.subLayerIds;&lt;BR /&gt; }&lt;BR /&gt; // Exclude some Line layers from the legend that are self-explanatory&lt;BR /&gt; if (legendLayerPrintLines.subLayerIds.length &amp;gt; 0) {&lt;BR /&gt; for (var i = 0; i &amp;lt; legendLayerPrintLines.subLayerIds.length; i++) {&lt;BR /&gt; if (legendLayerPrintLines.subLayerIds&lt;I&gt; == indexLine_RoadSegments || legendLayerPrintLines.subLayerIds&lt;I&gt; == indexLine_Waterline || legendLayerPrintLines.subLayerIds&lt;I&gt; == indexLine_Railroad) {&lt;BR /&gt; }&lt;BR /&gt; else {&lt;BR /&gt; visiblePrintSublayerLines.push(legendLayerPrintLines.subLayerIds&lt;I&gt;);&lt;BR /&gt; }&lt;BR /&gt; }&lt;BR /&gt; }&lt;BR /&gt; // Exclude some Polygon layers from the legend that are self-explanatory&lt;BR /&gt; if (legendLayerPrintPolygons.subLayerIds.length &amp;gt; 0) {&lt;BR /&gt; for (var j = 0; j &amp;lt; legendLayerPrintPolygons.subLayerIds.length; j++) {&lt;BR /&gt; if (legendLayerPrintPolygons.subLayerIds&lt;J&gt; == indexPoly_CountyBdry || legendLayerPrintPolygons.subLayerIds&lt;J&gt; == indexPoly_MuniBdry || legendLayerPrintPolygons.subLayerIds&lt;J&gt; == indexPoly_Waterbody) {&lt;BR /&gt; }&lt;BR /&gt; else {&lt;BR /&gt; visiblePrintSublayerPolygons.push(legendLayerPrintPolygons.subLayerIds&lt;J&gt;);&lt;BR /&gt; }&lt;BR /&gt; }&lt;BR /&gt; }&lt;/J&gt;&lt;/J&gt;&lt;/J&gt;&lt;/J&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;if ($('#chkPrintLegend').is(':checked')) {&lt;BR /&gt; legendLayerPrintPoints.subLayerIds = visiblePrintSublayerPoints;&lt;BR /&gt; legendLayerPrintLines.subLayerIds = visiblePrintSublayerLines;&lt;BR /&gt; legendLayerPrintPolygons.subLayerIds = visiblePrintSublayerPolygons;&lt;BR /&gt; }&lt;BR /&gt; else {&lt;BR /&gt; legendLayerPrintPoints.subLayerIds = [];&lt;BR /&gt; legendLayerPrintLines.subLayerIds = [];&lt;BR /&gt; legendLayerPrintPolygons.subLayerIds = [];&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;var printUrl = "&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fgisfarm.spokanecounty.org%2Farcgis%2Frest%2Fservices%2FUtilities%2FPrintingTools%2FGPServer%2FExport%2520Web%2520Map%2520Task" rel="nofollow" target="_blank"&gt;http://gisfarm.spokanecounty.org/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task&lt;/A&gt;&lt;SPAN&gt;";&lt;/SPAN&gt;&lt;BR /&gt; var printTask = new PrintTask(printUrl);&lt;BR /&gt; var template = new PrintTemplate();&lt;BR /&gt; template.format = "PDF";&lt;BR /&gt; template.layout = $('#dlPrintTemplates').val();&lt;BR /&gt; template.layoutOptions = {&lt;BR /&gt; legendLayers: [legendLayerPrintPoints, legendLayerPrintLines, legendLayerPrintPolygons],&lt;BR /&gt; titleText: customHeader,&lt;BR /&gt; scalebarUnit: "Feet"&lt;BR /&gt; };&lt;BR /&gt; template.preserveScale = printToScale;&lt;BR /&gt; template.outScale = customScale;&lt;BR /&gt; var params = new PrintParameters();&lt;BR /&gt; params.map = map;&lt;BR /&gt; params.template = template;&lt;BR /&gt; printTask.execute(params, getPrinted, getPrintError);&lt;/P&gt;&lt;P&gt;function getPrinted(evt) {&lt;BR /&gt; window.open(evt.url);&lt;BR /&gt; $(document.body).css({ 'cursor': 'pointer' });&lt;BR /&gt; map.setMapCursor("pointer");&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;function getPrintError(evt) {&lt;BR /&gt; alert(evt);&lt;BR /&gt; $(document.body).css({ 'cursor': 'pointer' });&lt;BR /&gt; map.setMapCursor("pointer");&lt;BR /&gt; }&lt;BR /&gt; }); // end btn-Print click function&lt;/P&gt;&lt;P&gt;// ***************************************************************&lt;BR /&gt; // first print ; need to execute this on map load to avoid a print error&lt;BR /&gt; // ***************************************************************&lt;BR /&gt; function firstPrint() {&lt;BR /&gt; var legendLayer = new esri.tasks.LegendLayer();&lt;BR /&gt; legendLayer.layerId = "pointsLayer";&lt;BR /&gt; legendLayer.subLayerIds = [4];&lt;BR /&gt; //alert(e.target.id);&lt;BR /&gt;&lt;SPAN&gt; printUrl = "&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fgisfarm.spokanecounty.org%2Farcgis%2Frest%2Fservices%2FPublicWorks%2FCustomPrintLayouts%2FGPServer%2FExport%2520Web%2520Map" rel="nofollow" target="_blank"&gt;http://gisfarm.spokanecounty.org/arcgis/rest/services/PublicWorks/CustomPrintLayouts/GPServer/Export%20Web%20Map&lt;/A&gt;&lt;SPAN&gt;";&lt;/SPAN&gt;&lt;BR /&gt; var printTask = new PrintTask(printUrl);&lt;BR /&gt; var template = new PrintTemplate();&lt;BR /&gt; //var nparcel = ;&lt;BR /&gt; template.format = "PDF";&lt;BR /&gt; template.layout = "ParcelLayoutLandscape";&lt;BR /&gt; template.layoutOptions = {&lt;BR /&gt; legendLayers: []&lt;BR /&gt; };&lt;BR /&gt; template.preserveScale = true;&lt;BR /&gt; var params = new PrintParameters();&lt;BR /&gt; params.map = map;&lt;BR /&gt; params.template = template;&lt;BR /&gt; printTask.execute(params, getPrinted, getPrintError);&lt;/P&gt;&lt;P&gt;function getPrinted(evt) {&lt;BR /&gt; //window.open(evt.url);&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;function getPrintError(evt) {&lt;BR /&gt; alert(evt);&lt;BR /&gt; }&lt;BR /&gt; // do something on button click&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;}); // end require&lt;BR /&gt; }); // end document ready&lt;/P&gt;&lt;P&gt;&amp;lt;/script&amp;gt;&lt;BR /&gt;&amp;lt;/head&amp;gt;&lt;BR /&gt;&amp;lt;body&amp;gt;&lt;BR /&gt; &amp;lt;div id="ui-esri-map"&amp;gt;&amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;div id="info"&amp;gt;&lt;BR /&gt; &amp;lt;div id="legendDiv"&amp;gt;&amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;div id="infoPrint"&amp;gt;&lt;BR /&gt; &amp;lt;div id="PrintDiv"&amp;gt;&lt;BR /&gt; &amp;lt;div&amp;gt;Basic Print: To print the map with added graphics select Template from Print Template list. To include custom header or print at specficied scale select Template then enter those values.&amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;!--&amp;lt;input type="submit" value="Print" id="btnPrint" data-mini="true" data-corners="false" /&amp;gt;--&amp;gt;&lt;BR /&gt; &amp;lt;!--&amp;lt;div id="btnBasicPrint"&amp;gt;&amp;lt;/div&amp;gt;--&amp;gt;&lt;BR /&gt; &amp;lt;br&amp;gt;&lt;BR /&gt; &amp;lt;div&amp;gt;&lt;BR /&gt; &amp;lt;label for="txtPrintTemplates"&amp;gt;Print Templates&amp;lt;/label&amp;gt;&lt;BR /&gt; &amp;lt;select id="dlPrintTemplates" style="width:100%;"&amp;gt;&lt;BR /&gt; &amp;lt;option value="Letter ANSI A Landscape"&amp;gt;Letter Landscape(PDF)&amp;lt;/option&amp;gt;&lt;BR /&gt; &amp;lt;option value="Letter ANSI A Portrait"&amp;gt;Letter Portrait(PDF)&amp;lt;/option&amp;gt;&lt;BR /&gt; &amp;lt;option value="MAP_ONLY"&amp;gt;Map Only Landscape(PNG)&amp;lt;/option&amp;gt;&lt;BR /&gt; &amp;lt;option value="Tabloid ANSI B Landscape"&amp;gt;Tabloid Landscape(PDF)&amp;lt;/option&amp;gt;&lt;BR /&gt; &amp;lt;option value="Tabloid ANSI B Portrait"&amp;gt;Tabloid Portrait(PDF)&amp;lt;/option&amp;gt;&lt;BR /&gt; &amp;lt;/select&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;div&amp;gt;&lt;BR /&gt; &amp;lt;label for="txtCustomHeader"&amp;gt;Custom Header&amp;lt;/label&amp;gt;&lt;BR /&gt; &amp;lt;input id="txtCustomHeader" placeholder="Enter custom Header..." style="width:100%;" /&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;div&amp;gt;&lt;BR /&gt; &amp;lt;input id="chkPrintScale" type="checkbox" /&amp;gt;&lt;BR /&gt; &amp;lt;label for="chkPrintScale"&amp;gt;Print to Scale&amp;lt;/label&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;div&amp;gt;&lt;BR /&gt; &amp;lt;label for="txtCustomScale"&amp;gt;Custom Scale&amp;lt;/label&amp;gt;&lt;BR /&gt; &amp;lt;input id="txtCustomScale" placeholder="Enter print Scale in inches" style="width:100%;" /&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;div&amp;gt;&lt;BR /&gt; &amp;lt;input id="chkPrintLegend" type="checkbox" /&amp;gt;&lt;BR /&gt; &amp;lt;label for="chkPrintLegend"&amp;gt;Print Legend&amp;lt;/label&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;input type="submit" value="Print" id="btnPrint" /&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt;&amp;lt;/body&amp;gt;&lt;BR /&gt;&amp;lt;/html&amp;gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 13 Jun 2017 21:14:52 GMT</pubDate>
    <dc:creator>LoriEmerson_McCormack</dc:creator>
    <dc:date>2017-06-13T21:14:52Z</dc:date>
    <item>
      <title>Setting legendLayer subLayerIds for PrintTask with Multiple Map Services</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/setting-legendlayer-sublayerids-for-printtask-with/m-p/155675#M14493</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have successfully gotten the PrintTask to work with multiple map services with a legend. &amp;nbsp;By hard-coding the list of legendLayer.subLayerIds, it works.&lt;/P&gt;&lt;P&gt;However, I want to look up the visible layers in the map and return an array of layer indexes and assign that array to the legendLayer.subLayerIds for each map service. &amp;nbsp;I have tried various things such as (1.) using a loop and pushing each array value into legendLayer.subLayerIds, (2.) using brackets (e.g., legendLayer.subLayerIds = [returnedArray], (3.) creating a string instead of an array with commas in between each layer index (e.g., "[38,44]"). &amp;nbsp;Option (3.) actually works, but even if I pass a blank string (e.g., "[ ]"), all of the visible layers for that map service are included in the print legend.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is the code that works with the hard-coded values for the legendLayer.subLayerIds:&lt;/P&gt;&lt;P&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;BR /&gt;&amp;lt;html&amp;gt;&lt;BR /&gt;&amp;lt;head&amp;gt;&lt;BR /&gt; &amp;lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&amp;gt;&lt;BR /&gt; &amp;lt;meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;lt;meta name="apple-mobile-web-app-capable" content="yes"&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;lt;meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;lt;meta name="mobile-web-app-capable" content="yes"&amp;gt;&lt;BR /&gt; &amp;lt;title&amp;gt;ArcGIS dynamic and tile layers using Popup and InfoTemplates&amp;lt;/title&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;lt;script type="text/javascript" src="../jquery/jquery-1.11.3.min.js"&amp;gt;&amp;lt;/script&amp;gt;&lt;BR /&gt; &amp;lt;!--&amp;lt;script type="text/javascript" src="../jquery-ui-1.10.4/jquery-ui-1.10.4/ui/jquery-ui.js"&amp;gt;&amp;lt;/script&amp;gt;--&amp;gt;&lt;BR /&gt; &amp;lt;script type="text/javascript" src="../jquery/jquery-ui-1.10.4/ui/jquery-ui.js"&amp;gt;&amp;lt;/script&amp;gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;lt;link rel="stylesheet" href="&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fjs.arcgis.com%2F3.20%2Fesri%2Fcss%2Fesri.css" rel="nofollow" target="_blank"&gt;https://js.arcgis.com/3.20/esri/css/esri.css&lt;/A&gt;&lt;SPAN&gt;"&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt; &amp;lt;style&amp;gt;&lt;BR /&gt; html, body, #ui-esri-map {&lt;BR /&gt; width: 100%;&lt;BR /&gt; height: 100%;&lt;BR /&gt; margin: 0;&lt;BR /&gt; padding: 0;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; .esriPopup .titlePane {&lt;BR /&gt; text-shadow: none;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;.esriPopup .titleButton.next {&lt;BR /&gt; right: 40px;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;.esriPopup .titleButton.prev {&lt;BR /&gt; right: 53px;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;.demographicInfoContent {&lt;BR /&gt; padding-top: 10px;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;.demographicInnerSpacing {&lt;BR /&gt; display: inline-block;&lt;BR /&gt; width: 8px;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;.demographicNumericPadding {&lt;BR /&gt; width: 90px;&lt;BR /&gt; display: inline-block;&lt;BR /&gt; text-align: right;&lt;BR /&gt; }&lt;BR /&gt; #info{&lt;BR /&gt; top: 0px;&lt;BR /&gt; right: 0px;&lt;BR /&gt; position: absolute;&lt;BR /&gt; z-index: 99;&lt;BR /&gt; opacity: 0.9;&lt;BR /&gt; background-color: whitesmoke;&lt;BR /&gt; border-bottom-left-radius: 8px;&lt;BR /&gt; padding: 0px 0px 0px 10px;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;#infoPrint{&lt;BR /&gt; bottom: 5px;&lt;BR /&gt; left: 5px;&lt;BR /&gt; position: absolute;&lt;BR /&gt; z-index: 99;&lt;BR /&gt; opacity: 0.9;&lt;BR /&gt; background-color: lightpink;&lt;BR /&gt; border-top-right-radius: 8px;&lt;BR /&gt; padding: 10px 10px 20px 10px;&lt;BR /&gt; width: 20%;&lt;BR /&gt; }&lt;BR /&gt; &amp;lt;/style&amp;gt;&lt;BR /&gt;&lt;SPAN&gt; &amp;lt;script src="&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fjs.arcgis.com%2F3.20%2F" rel="nofollow" target="_blank"&gt;https://js.arcgis.com/3.20/&lt;/A&gt;&lt;SPAN&gt;"&amp;gt;&amp;lt;/script&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt; &amp;lt;script&amp;gt;&lt;BR /&gt; $(document).ready(function () {&lt;BR /&gt; var map;&lt;BR /&gt; // layer indexes in the map services&lt;BR /&gt; var indexPoint_StormwaterOuterZoom = 34;&lt;BR /&gt; var indexPoint_StormwaterMiddleZoom = 35;&lt;BR /&gt; var indexPoint_StormwaterCloseUpZoom = 36;&lt;BR /&gt; var indexPoint_ControlPts = 1;&lt;BR /&gt; var indexPoint_FiberPts = 9;&lt;BR /&gt; var indexPoint_ReferencePts = 11;&lt;BR /&gt; var indexPoint_SchoolBusStopsOSPI = 19;&lt;BR /&gt; var indexPoint_AvistaPoleStructure = 28;&lt;BR /&gt; var indexPoint_Shop = 5;&lt;BR /&gt; var indexLine_PriorityArray = 13;&lt;BR /&gt; var indexLine_RoadSegments = 38;&lt;BR /&gt; var indexLine_Railroad = 41;&lt;BR /&gt; var indexLine_Waterline = 44;&lt;BR /&gt; var indexPoly_NoTrucks = 32;&lt;BR /&gt; var indexPoly_CountyBdry = 13;&lt;BR /&gt; var indexPoly_Parcels = 53;&lt;BR /&gt; var indexPoly_MuniBdry = 83;&lt;BR /&gt; var indexPoly_Waterbody = 27;&lt;BR /&gt; require([&lt;BR /&gt; "dojo/dom-construct",&lt;BR /&gt; "esri/Color",&lt;BR /&gt; "esri/dijit/Popup",&lt;BR /&gt; "esri/InfoTemplate",&lt;BR /&gt; "esri/dijit/Legend",&lt;BR /&gt; "esri/layers/ArcGISDynamicMapServiceLayer",&lt;BR /&gt; "esri/map",&lt;BR /&gt; "esri/tasks/PrintTask",&lt;BR /&gt; "esri/tasks/PrintTemplate",&lt;BR /&gt; "esri/tasks/PrintParameters",&lt;BR /&gt; "esri/tasks/LegendLayer",&lt;BR /&gt; "esri/config",&lt;BR /&gt; "dojo/parser",&lt;BR /&gt; "dojo/domReady!"&lt;BR /&gt; ], function (&lt;BR /&gt; domConstruct, Color, Popup, InfoTemplate, Legend,&lt;BR /&gt; ArcGISDynamicMapServiceLayer, Map,&lt;BR /&gt; PrintTask, PrintTemplate, PrintParameters, LegendLayer,&lt;BR /&gt; esriConfig,&lt;BR /&gt; parser&lt;BR /&gt; ) {&lt;/P&gt;&lt;P&gt;parser.parse();&lt;BR /&gt; //This sample may require a proxy page to handle communications with the ArcGIS Server services. You will need to&lt;BR /&gt; //replace the url below with the location of a proxy on your machine. See the 'Using the proxy page' help topic&lt;BR /&gt; //for details on setting up a proxy page.&lt;BR /&gt; esriConfig.defaults.io.proxyUrl = "/proxy/";&lt;BR /&gt; esriConfig.defaults.io.alwaysUseProxy = false;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; var initialExtent = new esri.geometry.Extent(&lt;BR /&gt; -117.83, 47.23,&lt;BR /&gt; -117.03, 48.055,&lt;BR /&gt; new esri.SpatialReference({ wkid: 4326 })&lt;BR /&gt; );&lt;/P&gt;&lt;P&gt;map = new Map("ui-esri-map", {&lt;BR /&gt; basemap: "topo",&lt;BR /&gt; extent: initialExtent,&lt;BR /&gt; sliderStyle: "small"&lt;BR /&gt; });&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;var polygonsLayer = new ArcGISDynamicMapServiceLayer("&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Figis.spokanecounty.org%2Farcgis%2Frest%2Fservices%2FPublicWorks%2FPublicWorksQueriesPolygons%2FMapServer" rel="nofollow" target="_blank"&gt;http://igis.spokanecounty.org/arcgis/rest/services/PublicWorks/PublicWorksQueriesPolygons/MapServer&lt;/A&gt;&lt;SPAN&gt;", {&lt;/SPAN&gt;&lt;BR /&gt; "id": "polygonsLayer",&lt;BR /&gt; "opacity": 0.75&lt;BR /&gt; });&lt;/P&gt;&lt;P&gt;polygonsLayer.setVisibleLayers([53]);&lt;BR /&gt; map.addLayer(polygonsLayer);&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;SPAN&gt; var linesLayer = new ArcGISDynamicMapServiceLayer("&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Figis.spokanecounty.org%2Farcgis%2Frest%2Fservices%2FPublicWorks%2FPublicWorksQueriesLines%2FMapServer" rel="nofollow" target="_blank"&gt;http://igis.spokanecounty.org/arcgis/rest/services/PublicWorks/PublicWorksQueriesLines/MapServer&lt;/A&gt;&lt;SPAN&gt;", {&lt;/SPAN&gt;&lt;BR /&gt; "id": "linesLayer",&lt;BR /&gt; "opacity": 0.75&lt;BR /&gt; });&lt;/P&gt;&lt;P&gt;linesLayer.setVisibleLayers([38]);&lt;BR /&gt; map.addLayer(linesLayer);&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;var pointsLayerURL = "&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Figis.spokanecounty.org%2Farcgis%2Frest%2Fservices%2FPublicWorks%2FPublicWorksQueriesPoints%2FMapServer" rel="nofollow" target="_blank"&gt;http://igis.spokanecounty.org/arcgis/rest/services/PublicWorks/PublicWorksQueriesPoints/MapServer&lt;/A&gt;&lt;SPAN&gt;";&lt;/SPAN&gt;&lt;BR /&gt; var pointsLayerOptions = {&lt;BR /&gt; "id": "pointsLayer",&lt;BR /&gt; "opacity": 0.8,&lt;BR /&gt; "showAttribution": false&lt;BR /&gt; };&lt;BR /&gt; var pointsLayer = new ArcGISDynamicMapServiceLayer(pointsLayerURL, pointsLayerOptions);&lt;/P&gt;&lt;P&gt;pointsLayer.setVisibleLayers([4, 5]);&lt;BR /&gt; map.addLayer(pointsLayer);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;map.on("load", function (evt) {&lt;BR /&gt; var legend = new Legend({&lt;BR /&gt; map: map,&lt;BR /&gt; layerInfos: [{&lt;BR /&gt; layer: linesLayer,&lt;BR /&gt; title: "Lines Layer"&lt;BR /&gt; },&lt;BR /&gt; {&lt;BR /&gt; layer: pointsLayer,&lt;BR /&gt; hideLayers: [4],&lt;BR /&gt; title: "Points Layer"&lt;BR /&gt; }]&lt;BR /&gt; }, "legendDiv");&lt;/P&gt;&lt;P&gt;legend.startup();&lt;/P&gt;&lt;P&gt;firstPrint();&lt;BR /&gt; });&lt;/P&gt;&lt;P&gt;var customHeader = 'Spokane County Public Works';&lt;BR /&gt; var customScale;&lt;BR /&gt; var printToScale;&lt;/P&gt;&lt;P&gt;// ***************************************************************&lt;BR /&gt; // btnPrint click function&lt;BR /&gt; // ***************************************************************&lt;BR /&gt; $('#btnPrint').on('click', function () {&lt;BR /&gt; $(document.body).css({ 'cursor': 'wait' });&lt;BR /&gt; map.setMapCursor("wait");&lt;BR /&gt; if ($('#txtCustomHeader').val().length &amp;lt; 1) {&lt;BR /&gt; customHeader = 'Spokane County Public Works'&lt;BR /&gt; }&lt;BR /&gt; else {&lt;BR /&gt; customHeader = $('#txtCustomHeader').val();&lt;BR /&gt; }&lt;BR /&gt; if ($('#chkPrintScale').is(':checked')) {&lt;BR /&gt; printToScale = true;&lt;BR /&gt; }&lt;BR /&gt; else {&lt;BR /&gt; printToScale = false;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;if ($('#txtCustomScale').val().length &amp;lt; 1) {&lt;BR /&gt; customScale = map.getScale();&lt;BR /&gt; }&lt;BR /&gt; else {&lt;BR /&gt; customScale = $('#txtCustomScale').val();&lt;BR /&gt; }&lt;BR /&gt; // points legend&lt;BR /&gt; var legendLayerPrintPoints = new LegendLayer();&lt;BR /&gt; legendLayerPrintPoints.layerId = "pointsLayer";&lt;BR /&gt; legendLayerPrintPoints.subLayerIds = [indexPoint_Shop];&lt;BR /&gt; // lines legend&lt;BR /&gt; var legendLayerPrintLines = new LegendLayer();&lt;BR /&gt; legendLayerPrintLines.layerId = "linesLayer";&lt;BR /&gt; legendLayerPrintLines.subLayerIds = [indexLine_RoadSegments];&lt;BR /&gt; // polygons legend&lt;BR /&gt; var legendLayerPrintPolygons = new LegendLayer();&lt;BR /&gt; legendLayerPrintPolygons.layerId = "polygonsLayer";&lt;BR /&gt; legendLayerPrintPolygons.subLayerIds = [indexPoly_CountyBdry, indexPoly_Parcels, indexPoly_MuniBdry];&lt;/P&gt;&lt;P&gt;var visiblePrintSublayerPoints = [], visiblePrintSublayerLines = [], visiblePrintSublayerPolygons = [];&lt;BR /&gt; // No need to exclude any Point layers from the legend&lt;BR /&gt; if (legendLayerPrintPoints.subLayerIds.length &amp;gt; 0) {&lt;BR /&gt; visiblePrintSublayerPoints = legendLayerPrintPoints.subLayerIds;&lt;BR /&gt; }&lt;BR /&gt; // Exclude some Line layers from the legend that are self-explanatory&lt;BR /&gt; if (legendLayerPrintLines.subLayerIds.length &amp;gt; 0) {&lt;BR /&gt; for (var i = 0; i &amp;lt; legendLayerPrintLines.subLayerIds.length; i++) {&lt;BR /&gt; if (legendLayerPrintLines.subLayerIds&lt;I&gt; == indexLine_RoadSegments || legendLayerPrintLines.subLayerIds&lt;I&gt; == indexLine_Waterline || legendLayerPrintLines.subLayerIds&lt;I&gt; == indexLine_Railroad) {&lt;BR /&gt; }&lt;BR /&gt; else {&lt;BR /&gt; visiblePrintSublayerLines.push(legendLayerPrintLines.subLayerIds&lt;I&gt;);&lt;BR /&gt; }&lt;BR /&gt; }&lt;BR /&gt; }&lt;BR /&gt; // Exclude some Polygon layers from the legend that are self-explanatory&lt;BR /&gt; if (legendLayerPrintPolygons.subLayerIds.length &amp;gt; 0) {&lt;BR /&gt; for (var j = 0; j &amp;lt; legendLayerPrintPolygons.subLayerIds.length; j++) {&lt;BR /&gt; if (legendLayerPrintPolygons.subLayerIds&lt;J&gt; == indexPoly_CountyBdry || legendLayerPrintPolygons.subLayerIds&lt;J&gt; == indexPoly_MuniBdry || legendLayerPrintPolygons.subLayerIds&lt;J&gt; == indexPoly_Waterbody) {&lt;BR /&gt; }&lt;BR /&gt; else {&lt;BR /&gt; visiblePrintSublayerPolygons.push(legendLayerPrintPolygons.subLayerIds&lt;J&gt;);&lt;BR /&gt; }&lt;BR /&gt; }&lt;BR /&gt; }&lt;/J&gt;&lt;/J&gt;&lt;/J&gt;&lt;/J&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;if ($('#chkPrintLegend').is(':checked')) {&lt;BR /&gt; legendLayerPrintPoints.subLayerIds = visiblePrintSublayerPoints;&lt;BR /&gt; legendLayerPrintLines.subLayerIds = visiblePrintSublayerLines;&lt;BR /&gt; legendLayerPrintPolygons.subLayerIds = visiblePrintSublayerPolygons;&lt;BR /&gt; }&lt;BR /&gt; else {&lt;BR /&gt; legendLayerPrintPoints.subLayerIds = [];&lt;BR /&gt; legendLayerPrintLines.subLayerIds = [];&lt;BR /&gt; legendLayerPrintPolygons.subLayerIds = [];&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;var printUrl = "&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fgisfarm.spokanecounty.org%2Farcgis%2Frest%2Fservices%2FUtilities%2FPrintingTools%2FGPServer%2FExport%2520Web%2520Map%2520Task" rel="nofollow" target="_blank"&gt;http://gisfarm.spokanecounty.org/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task&lt;/A&gt;&lt;SPAN&gt;";&lt;/SPAN&gt;&lt;BR /&gt; var printTask = new PrintTask(printUrl);&lt;BR /&gt; var template = new PrintTemplate();&lt;BR /&gt; template.format = "PDF";&lt;BR /&gt; template.layout = $('#dlPrintTemplates').val();&lt;BR /&gt; template.layoutOptions = {&lt;BR /&gt; legendLayers: [legendLayerPrintPoints, legendLayerPrintLines, legendLayerPrintPolygons],&lt;BR /&gt; titleText: customHeader,&lt;BR /&gt; scalebarUnit: "Feet"&lt;BR /&gt; };&lt;BR /&gt; template.preserveScale = printToScale;&lt;BR /&gt; template.outScale = customScale;&lt;BR /&gt; var params = new PrintParameters();&lt;BR /&gt; params.map = map;&lt;BR /&gt; params.template = template;&lt;BR /&gt; printTask.execute(params, getPrinted, getPrintError);&lt;/P&gt;&lt;P&gt;function getPrinted(evt) {&lt;BR /&gt; window.open(evt.url);&lt;BR /&gt; $(document.body).css({ 'cursor': 'pointer' });&lt;BR /&gt; map.setMapCursor("pointer");&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;function getPrintError(evt) {&lt;BR /&gt; alert(evt);&lt;BR /&gt; $(document.body).css({ 'cursor': 'pointer' });&lt;BR /&gt; map.setMapCursor("pointer");&lt;BR /&gt; }&lt;BR /&gt; }); // end btn-Print click function&lt;/P&gt;&lt;P&gt;// ***************************************************************&lt;BR /&gt; // first print ; need to execute this on map load to avoid a print error&lt;BR /&gt; // ***************************************************************&lt;BR /&gt; function firstPrint() {&lt;BR /&gt; var legendLayer = new esri.tasks.LegendLayer();&lt;BR /&gt; legendLayer.layerId = "pointsLayer";&lt;BR /&gt; legendLayer.subLayerIds = [4];&lt;BR /&gt; //alert(e.target.id);&lt;BR /&gt;&lt;SPAN&gt; printUrl = "&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fgisfarm.spokanecounty.org%2Farcgis%2Frest%2Fservices%2FPublicWorks%2FCustomPrintLayouts%2FGPServer%2FExport%2520Web%2520Map" rel="nofollow" target="_blank"&gt;http://gisfarm.spokanecounty.org/arcgis/rest/services/PublicWorks/CustomPrintLayouts/GPServer/Export%20Web%20Map&lt;/A&gt;&lt;SPAN&gt;";&lt;/SPAN&gt;&lt;BR /&gt; var printTask = new PrintTask(printUrl);&lt;BR /&gt; var template = new PrintTemplate();&lt;BR /&gt; //var nparcel = ;&lt;BR /&gt; template.format = "PDF";&lt;BR /&gt; template.layout = "ParcelLayoutLandscape";&lt;BR /&gt; template.layoutOptions = {&lt;BR /&gt; legendLayers: []&lt;BR /&gt; };&lt;BR /&gt; template.preserveScale = true;&lt;BR /&gt; var params = new PrintParameters();&lt;BR /&gt; params.map = map;&lt;BR /&gt; params.template = template;&lt;BR /&gt; printTask.execute(params, getPrinted, getPrintError);&lt;/P&gt;&lt;P&gt;function getPrinted(evt) {&lt;BR /&gt; //window.open(evt.url);&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;function getPrintError(evt) {&lt;BR /&gt; alert(evt);&lt;BR /&gt; }&lt;BR /&gt; // do something on button click&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;}); // end require&lt;BR /&gt; }); // end document ready&lt;/P&gt;&lt;P&gt;&amp;lt;/script&amp;gt;&lt;BR /&gt;&amp;lt;/head&amp;gt;&lt;BR /&gt;&amp;lt;body&amp;gt;&lt;BR /&gt; &amp;lt;div id="ui-esri-map"&amp;gt;&amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;div id="info"&amp;gt;&lt;BR /&gt; &amp;lt;div id="legendDiv"&amp;gt;&amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;div id="infoPrint"&amp;gt;&lt;BR /&gt; &amp;lt;div id="PrintDiv"&amp;gt;&lt;BR /&gt; &amp;lt;div&amp;gt;Basic Print: To print the map with added graphics select Template from Print Template list. To include custom header or print at specficied scale select Template then enter those values.&amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;!--&amp;lt;input type="submit" value="Print" id="btnPrint" data-mini="true" data-corners="false" /&amp;gt;--&amp;gt;&lt;BR /&gt; &amp;lt;!--&amp;lt;div id="btnBasicPrint"&amp;gt;&amp;lt;/div&amp;gt;--&amp;gt;&lt;BR /&gt; &amp;lt;br&amp;gt;&lt;BR /&gt; &amp;lt;div&amp;gt;&lt;BR /&gt; &amp;lt;label for="txtPrintTemplates"&amp;gt;Print Templates&amp;lt;/label&amp;gt;&lt;BR /&gt; &amp;lt;select id="dlPrintTemplates" style="width:100%;"&amp;gt;&lt;BR /&gt; &amp;lt;option value="Letter ANSI A Landscape"&amp;gt;Letter Landscape(PDF)&amp;lt;/option&amp;gt;&lt;BR /&gt; &amp;lt;option value="Letter ANSI A Portrait"&amp;gt;Letter Portrait(PDF)&amp;lt;/option&amp;gt;&lt;BR /&gt; &amp;lt;option value="MAP_ONLY"&amp;gt;Map Only Landscape(PNG)&amp;lt;/option&amp;gt;&lt;BR /&gt; &amp;lt;option value="Tabloid ANSI B Landscape"&amp;gt;Tabloid Landscape(PDF)&amp;lt;/option&amp;gt;&lt;BR /&gt; &amp;lt;option value="Tabloid ANSI B Portrait"&amp;gt;Tabloid Portrait(PDF)&amp;lt;/option&amp;gt;&lt;BR /&gt; &amp;lt;/select&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;div&amp;gt;&lt;BR /&gt; &amp;lt;label for="txtCustomHeader"&amp;gt;Custom Header&amp;lt;/label&amp;gt;&lt;BR /&gt; &amp;lt;input id="txtCustomHeader" placeholder="Enter custom Header..." style="width:100%;" /&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;div&amp;gt;&lt;BR /&gt; &amp;lt;input id="chkPrintScale" type="checkbox" /&amp;gt;&lt;BR /&gt; &amp;lt;label for="chkPrintScale"&amp;gt;Print to Scale&amp;lt;/label&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;div&amp;gt;&lt;BR /&gt; &amp;lt;label for="txtCustomScale"&amp;gt;Custom Scale&amp;lt;/label&amp;gt;&lt;BR /&gt; &amp;lt;input id="txtCustomScale" placeholder="Enter print Scale in inches" style="width:100%;" /&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;div&amp;gt;&lt;BR /&gt; &amp;lt;input id="chkPrintLegend" type="checkbox" /&amp;gt;&lt;BR /&gt; &amp;lt;label for="chkPrintLegend"&amp;gt;Print Legend&amp;lt;/label&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;input type="submit" value="Print" id="btnPrint" /&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt; &amp;lt;/div&amp;gt;&lt;BR /&gt;&amp;lt;/body&amp;gt;&lt;BR /&gt;&amp;lt;/html&amp;gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 Jun 2017 21:14:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/setting-legendlayer-sublayerids-for-printtask-with/m-p/155675#M14493</guid>
      <dc:creator>LoriEmerson_McCormack</dc:creator>
      <dc:date>2017-06-13T21:14:52Z</dc:date>
    </item>
    <item>
      <title>Re: Setting legendLayer subLayerIds for PrintTask with Multiple Map Services</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/setting-legendlayer-sublayerids-for-printtask-with/m-p/155676#M14494</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The problem was that the array I was building considered the numbers as string rather than integer, so I had to use &lt;STRONG&gt;parseInt.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;I should have included this code in my original post. &amp;nbsp;I thought I was misunderstanding the subLayerIds String[] type.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function getPrintLegendLayerPolygons() {&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;var arrayLegendPolygons = [], visiblePrintSublayerPolygons = [], arrayValue = [];&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;arrayLegendPolygons = getVisibleLayersPolygons().sort();&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;// Exclude some Polygon layers from the legend that are self-explanatory&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;if (arrayLegendPolygons.length &amp;gt; 0) {&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for (var i = 0; i &amp;lt; arrayLegendPolygons.length; i++) {&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (arrayLegendPolygons&lt;I&gt; == indexPoly_CountyBdry || arrayLegendPolygons&lt;I&gt; == indexPoly_MuniBdry || &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;arrayLegendPolygons&lt;I&gt; == indexPoly_Waterbody) {&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else {&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;visiblePrintSublayerPolygons.push&lt;STRONG&gt;(parseInt(arrayLegendPolygons&lt;I&gt;,10))&lt;/I&gt;&lt;/STRONG&gt;&lt;I&gt;;&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;arrayValue = visiblePrintSublayerPolygons;&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;else {&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;arrayValue = [];&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;return arrayValue;&lt;BR /&gt; }&amp;nbsp;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function getVisibleLayersPolygons() {&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;visiblePolygons = [];&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;$('[name="layerVisibility"]:checked').each(function () {&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if ($(this).is(':checked')) {&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;var layerid = $(this).attr('id').split(":");&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// layerid[1] is LayerIndex in the Map Service&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// layerid[3] is service layer (e.g., points, lines, polygons)&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (layerid[3] == 'Polygons') {&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;visiblePolygons.push(layerid[1]);&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;} &lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;} // end if stmt&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;});&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;return visiblePolygons;&lt;BR /&gt; }&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 23 Jun 2017 16:33:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/setting-legendlayer-sublayerids-for-printtask-with/m-p/155676#M14494</guid>
      <dc:creator>LoriEmerson_McCormack</dc:creator>
      <dc:date>2017-06-23T16:33:55Z</dc:date>
    </item>
  </channel>
</rss>

