How to access maps and layers via the MxDocument


Summary
MxDocument is the class representing the map document in ArcMap. This makes the class a central point to ArcMap customization. This topic demonstrates how to access the maps and layers of a map document via IMxDocument.

Accessing maps and layers via the MxDocument

In the following code example, the maps and layers are looped through and the names of these objects are reported to the console. Do the following steps to access maps and layers:
  1. Get the maps from the MxDocument.
[C#]
IMap pMap;
IMaps pMaps = pMxDocument.Maps;
[VB.NET]
Dim pMap As IMap
Dim pMaps As IMaps = pMxDocument.Maps
  1. Enumerate through the maps and output the name of each map to the console. See the following code example:
[C#]
for (int i = 0; i <= pMaps.Count - 1; i++)
{
    pMap = pMaps.get_Item(i);
    Console.WriteLine(pMap.Name);
    IEnumLayer pEnumLayer = pMap.get_Layers(null, true);
    pEnumLayer.Reset();
    ILayer pLayer = pEnumLayer.Next();
    while (pLayer != null)
    {
        Console.WriteLine(pLayer.Name);
        pLayer = pEnumLayer.Next();
    }
}
[VB.NET]
Dim i As Integer
For i = 0 To pMaps.Count - 1 Step i + 1
    pMap = pMaps.Item(i)
    Console.WriteLine(pMap.Name)
    Dim pEnumLayer As IEnumLayer = pMap.Layers(Nothing, True)
    pEnumLayer.Reset()
    Dim pLayer As ILayer = pEnumLayer.Next()
    While Not pLayer Is Nothing
        Console.WriteLine(pLayer.Name)
        
        pLayer = pEnumLayer.Next()
    End While
    
Next






Additional Requirements
  • pMxDocument—An IMxDocument referencing the map document that you want to access.

Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced