How to add a layer to a map from a layer file


Summary
This article illustrates how to open a layer file and add the layer to a map in the code.

Adding a layer to a map from a layer file

This operation is performed using the MapDocument class, which can open layer files, as well as map documents (.mxds) and published map files (.pmfs). See the following code:
[Java]
static void addLayerFileToMap(IMap map, String path)throws Exception{
    IMapDocument document = new MapDocument();
    //Use the map document class to open the layer.
    document.open(path, "");
    //When opening a layer with the map document class,
    //the layer is added to a new document as the first
    //layer in the first map.
    map.addLayer(document.getMap(0).getLayer(0));
}






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
Engine Developer Kit Engine