PrintTask and the legendLayers option

Discussion created by jkressin on Jan 22, 2013
Latest reply on Jan 25, 2013 by jkressin

I have an application that is consuming multiple dynamic map services.  The print functionality is working correctly at creating maps out of these services.  I am just having a bit of trouble with the legendLayers option.  If I don't set it, the legend in my template shows legend items for all visible layers.  I want to cut this down to a specific subset of layers.  I can generate an array of LegendLayer items, but I don't get anything in the legend when I go this route.  I am guessing what I am doing wrong is setting the layerId to the name as listed in the mxd.  What value should I use for this ID when using multiple dynamic services.

To not get too complicated, but here is a snippet of the code showing what I am trying to do.  The rest of the template information is configured earlier on:

print_map: function (template, title) {
        var params = new esri.tasks.PrintParameters();
        var legend_layers = this.get_legend_layers();
        params.map = this.map;
        for (var x = 0; x < this.templates.length; x++) {
            if (this.templates[x].label == template)
                params.template = this.templates[x];
        params.template.layoutOptions.titleText = title;
        params.template.layoutOptions.legendLayers = legend_layers;
        var printtask = new esri.tasks.PrintTask(this.printService, params);
        printtask.execute(params, function (result) {
            var mywindow = window.open(result.url);
        }, function (err) { alert("Error printing map"); });

and the get_legend_layers function works like this:
get_legend_layers: function () {
        var legend_layers = Array();
        for (var x in this.defaults) {
            // Interate thru the application configuration structure (index is service id, contains info on service and individual layers)
            // Did we go in or out of scale
            var myservice = this.layers[x];
            if (myservice != null && myservice.visible == true) {
                var vis_layers = this.layers[x].visibleLayers;
                //iterate thru this services visible layers
                for (var y = 0; y < vis_layers.length; y++) {
                    var name = this.layers[x].layerInfos[vis_layers[y]].name.toString()
                    // Check with configuration settings that this layer is something we want in the legend
                    if (this.defaults[x][name].is_listed == true && this.defaults[x][name].attribute_list != "") {
                        var ll = new esri.tasks.LegendLayer();
                        ll.layerId = name;
        return (legend_layers);

Thanks for any advice you can provide on this.