ArcObjects Library Reference (Carto)  

IMapServer.GetLegendInfo Method

Returns a collection of Map Server Legend Info objects for the specified layers. If layerIDs is Nothing/Null or empty, legend information for all layers is returned.

[Visual Basic .NET]
Public Function GetLegendInfo ( _
    ByVal MapName As String, _
    ByVal layerIds As ILongArray, _
    ByVal patch As IMapServerLegendPatch, _
    ByVal imgType As IImageType _
) As IMapServerLegendInfos
[C#]
public IMapServerLegendInfos GetLegendInfo (
    string MapName,
    ILongArray layerIds,
    IMapServerLegendPatch patch,
    IImageType imgType
);
[C++]
HRESULT GetLegendInfo(
  BSTR MapName,
  ILongArray* layerIds,
  IMapServerLegendPatch* patch,
  IImageType* imgType,
  IMapServerLegendInfos** legendInfos
);
[C++]

Parameters

MapName [in]   MapName is a parameter of type BSTR layerIds [in]

  layerIds is a parameter of type ILongArray

patch [in]

  patch is a parameter of type IMapServerLegendPatch

imgType [in]

  imgType is a parameter of type IImageType

legendInfos [out, retval]

  legendInfos is a parameter of type IMapServerLegendInfos

Product Availability

Available with ArcGIS Engine, ArcGIS Desktop, and ArcGIS Server.

Remarks

Use GetLegendInfo to retrieve individual legend elements including the symbol image, labels, descriptions and headings. A common use would be to populate a table of contents. Note that composite layers such as group layers and annotation layers do not contain legend elements. To export a single image of the legend use ExportLegend on IMapServerLayout.

The GetLegendInfo method returns a collection of MapServerLegendInfo objects. Legends are associated with renderers that belong to each layer in a map. Each layer has a separate renderer. Each renderer has one or more legend groups.  Each legend group has one or more legend classes. Customizing the legend patch can be done using IMapServerLegendPatch . When passing in "Nothing" for this input parameter ("patch") the default legend patch is used.

MapServerLegendInfo doesn't contain information about whether the data frame and the layers in the TOC are expanded or collapsed in the original map document. You have to write your own code to find this out.

[C#]

The following sample code shows how to determine whether the data frame and the layers in the TOC are expanded or collapsed in the original map document. The sample assumes that you already have a valid MapServer object.

IMapServer mapServer = m_MapServer;
IMapServerObjects mapServerObjects;
IMapLayerInfos mapLayerInfos;
IMap map;
ILayer layer;
IGroupLayer groupLayer;
ICompositeLayer2 compLayer;
ILegendInfo legendInfo;
string strMapName;
int i, j, k;
Boolean bVisible;
string debugOutput;
// Step through data frames
for (i = 0; i < mapServer.MapCount; i++)
{
    // Get map name
        strMapName = mapServer.get_MapName(i);
        // Get map
        mapServerObjects = (IMapServerObjects)mapServer;
        map = mapServerObjects.get_Map(strMapName);
        // Print whether map (data frame) is expanded or collapsed
        debugOutput = "Map " + i.ToString() + " (" + strMapName + "): " + (map.Expanded ? "Expanded" : "Collapsed");
        MessageBox.Show(debugOutput);
        // Get MapLayerInfos
        mapLayerInfos = mapServer.GetServerInfo(strMapName).MapLayerInfos;
        // Step through layers
        for (j = 0; j < mapLayerInfos.Count; j++)
        {
                // Get layer
                layer = mapServerObjects.get_Layer(strMapName, j);
                // Check if group layer
                if (layer is IGroupLayer)
                {
                        groupLayer = (IGroupLayer)layer;
                        debugOutput = "Layer " + j.ToString() + " (" + layer.Name + "): " + (groupLayer.Expanded ? "Expanded" : "Collapsed");
                        MessageBox.Show(debugOutput);
                }
                // Check if composite layer
                else if (layer is ICompositeLayer2)
                {
                        compLayer = (ICompositeLayer2)layer;
                        debugOutput = "Layer " + j.ToString() + " (" + layer.Name + "): " + (compLayer.Expanded ? "Expanded" : "Collapsed");
                        MessageBox.Show(debugOutput);
                }
                else if (layer is ILegendInfo)
                {
                        legendInfo = (ILegendInfo)layer;
                        // If at least 1 legend group is visible, layer is expanded
                        bVisible = false;
                        for (k = 0; k < legendInfo.LegendGroupCount; k++)
                        {
                                if (legendInfo.get_LegendGroup(k).Visible)
                                {
                                        bVisible = true;
                                        break;
                                }
                        }
                        debugOutput = "Layer " + j.ToString() + " (" + layer.Name + "): " + (bVisible ? "Expanded" : "Collapsed");
                        MessageBox.Show(debugOutput);
                }
                else
                {
                        // All other layers that cannot be expanded or collapsed
                        // -> set them to "expanded"
                        debugOutput = "Layer " + j.ToString() + " (" + layer.Name + "): " + "Expanded";
                        MessageBox.Show(debugOutput);
                }
        }
}

See Also

IMapServer Interface