PrintTask LegendLayer Can not set the SubLayerIds

2990
2
11-14-2014 04:09 PM
MargoBlosser
New Contributor

I have a print service setup with custom layouts published with Export Web Map. The Layouts do not contain any layers, these are added in the PrintTask .  I have created a dictionary of all of the layers that are within my map extent named legendInfo.

For the legend I need to set the SublayerIds for the Legend Layer to be those that are within the map extent and are visible. This is done to minimize the size of the Legend on the pdf.

For the dictionary key value pair, the key is the ID of the ArcGISDynamicMapServiceLayer and the values are a List<int> of the subids for the ArcGISDynamicMapServiceLayer.

. I have tried several casts such as pair.Value as IEnumerable<List<int>>.

SublayerIds are null after the loop has finished.

Scratching my head...any suggestions? Thanks in advance...

Margo

         foreach (var pair in legendInfo)
                {
                    ESRI.ArcGIS.Client.Printing.LegendLayer myLegendLayer = new ESRI.ArcGIS.Client.Printing.LegendLayer();

                    // Set the LegendLayer.LayerId to the Layer.ID to add it to the printed map's legend area.

                    myLegendLayer.LayerId = pair.Key.ToString();                
                  
                    myLegendLayer.SubLayerIds = pair.Value as IEnumerable<Object>;
                  
                  
                    myLegendLayers.Add(myLegendLayer);
                } 



0 Kudos
2 Replies
DominiqueBroux
Esri Frequent Contributor

You can't cast directly a List<int> to an IEnumerable<object>.

Even if the Silverlight IEnumerable was supporting the covariance (which anyway is not the case unlike WPF version), that would not work since int is not an object.

You should get it to work by casting each int to an object. Something like:

myLegendLayer.SubLayerIds = pair.Value.Cast<object>();

0 Kudos
JenniferNery
Esri Regular Contributor

Hi Margo,

PrintTask already does this for you if you had chosen a Format other than MapOnly, if LayoutOptions.LegendOptions was provided and if the layers for printing have their ID property set.

You can use Linq query to iterate through ArcGISSDynamicMapSerivceLayer .VisibleLayers. Since it is different type for ArcGISSDynamicMapSerivceLayer (int) and WmsLayer (string), we opted to use object.

if (l.ShowLegend)
  {
  var legendLayer = new LegendLayer() { LayerId = l.ID };
  if (l is ArcGISDynamicMapServiceLayer)
  {
  if ((l as ArcGISDynamicMapServiceLayer).VisibleLayers != null)
  {
  legendLayer.SubLayerIds = from s in (l as ArcGISDynamicMapServiceLayer).VisibleLayers
   select s as object;
  }
  else if ((l as ArcGISDynamicMapServiceLayer).DynamicLayerInfos != null)
  {
  legendLayer.SubLayerIds = from s in (l as ArcGISDynamicMapServiceLayer).DynamicLayerInfos
   select s.ID as object;
  }
  else if ((l as ArcGISDynamicMapServiceLayer).Layers != null)
  {
  legendLayer.SubLayerIds = from s in (l as ArcGISDynamicMapServiceLayer).Layers
   select s.ID as object;
  }
  }
  else if (l is WmsLayer)
  {
  if ((l as WmsLayer).Layers != null)
  {
  legendLayer.SubLayerIds = from s in (l as WmsLayer).Layers
   select s as object;
  }
  else if ((l as WmsLayer).LayerList != null)
  {
  legendLayer.SubLayerIds = from s in (l as WmsLayer).LayerList
   select s.Name as object;
  }
  }
  legendLayers.Add(legendLayer);
  }
0 Kudos