ArcGIS Pro 3.0 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / Map Class / FindLayers Method
Name of the layer
(optional) When true the search continues in group layers. (default value = true)
Example

In This Topic
    FindLayers Method (Map)
    In This Topic
    Finds layers by name.
    Syntax
    Public Function FindLayers( _
       ByVal name As String, _
       Optional ByVal recursive As Boolean _
    ) As IReadOnlyList(Of Layer)

    Parameters

    name
    Name of the layer
    recursive
    (optional) When true the search continues in group layers. (default value = true)

    Return Value

    A read only list of Layers
    Example
    Find edit template by name on a layer
    ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
    {
      //get the templates
      var map = ArcGIS.Desktop.Mapping.MapView.Active.Map;
      if (map == null)
        return;
    
      var mainTemplate = map.FindLayers("main").FirstOrDefault()?.GetTemplate("Distribution");
      var mhTemplate = map.FindLayers("Manhole").FirstOrDefault()?.GetTemplate("Active");
    });
    MapFrame_SetCamera_Envelope
    //Set the extent of a map frame to the envelope of a feature.
    
    //Perform on the worker thread
    await QueuedTask.Run(() =>
    {
      //Reference MapFrame
      MapFrame mf_env = layout.FindElement("Map Frame") as MapFrame;
    
      //Get map and a layer of interest
      Map m = mf_env.Map;
      //Get the specific layer you want from the map and its extent
      FeatureLayer lyr = m.FindLayers("GreatLakes").First() as FeatureLayer;
      Envelope lyrEnv = lyr.QueryExtent();
    
      //Set the map frame extent to the feature layer's extent / envelope
      mf_env.SetCamera(lyrEnv);  //Note - you could have also used the lyr as an overload option
    });
    Create Table Frame
    //Create a table frame.
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //Build 2D envelope geometry
      Coordinate2D rec_ll = new Coordinate2D(1.0, 3.5);
      Coordinate2D rec_ur = new Coordinate2D(7.5, 4.5);
      //At 2.x - Envelope rec_env = EnvelopeBuilder.CreateEnvelope(rec_ll, rec_ur);
      Envelope rec_env = EnvelopeBuilderEx.CreateEnvelope(rec_ll, rec_ur);
    
      //Reference map frame and layer
      MapFrame mf = layout.FindElement("Map Frame") as MapFrame;
      FeatureLayer lyr = mf.Map.FindLayers("GreatLakes").First() as FeatureLayer;
    
      //Build fields list
      var fields = new[] { "NAME", "Shape_Area", "Shape_Length" };
    
      //Construct the table frame
      //At 2.x - TableFrame tabFrame = LayoutElementFactory.Instance.CreateTableFrame(
      //              layout, rec_env, mf, lyr, fields);
    
      var tableFrameInfo = new TableFrameInfo()
      {
        FieldNames = fields,
        MapFrameName = mf.Name,
        MapMemberUri = lyr.URI
      };
      var tabFrame = ElementFactory.Instance.CreateMapSurroundElement(
        layout, rec_env, tableFrameInfo) as TableFrame;
    });
    Zoom map frame to extent of a single layer
    //Zoom map frame to the extent of a single layer.
    
    //Perform on the worker thread
    await QueuedTask.Run(() =>
    {
      //Reference MapFrame
      MapFrame mf = layout.FindElement("Map Frame") as MapFrame;
    
      //Reference map and layer
      Map m = mf.Map;
      FeatureLayer lyr = m.FindLayers("GreatLakes").First() as FeatureLayer;
    
      //Set the map frame extent to all features in the layer
      mf.SetCamera(lyr, false);
    });
    Change map frame extent to selected features in multiple layers
    //Change the extent of a map frame to the selected features multiple layers.
    
    //Perform on the worker thread
    await QueuedTask.Run(() =>
    {
      //Reference MapFrame
      MapFrame mf = layout.FindElement("Map Frame") as MapFrame;
    
      //Reference map, layers and create layer list
      Map m = mf.Map;
      FeatureLayer fl_1 = m.FindLayers("GreatLakes").First() as FeatureLayer;
      FeatureLayer fl_2 = m.FindLayers("States_WithRegions").First() as FeatureLayer;
      var layers = new[] { fl_1, fl_2 };
      //IEnumerable<Layer> layers = m.Layers;  //This creates a list of ALL layers in map.
    
      //Set the map frame extent to the selected features in the list of layers
      mf.SetCamera(layers, true);
    });
    Change map frame extent to single feature with 15 percent buffer
    //Change map frame extent to single feature with 10 percent buffer
    
    //Process on the worker thread
    await QueuedTask.Run(() =>
    {
      //Reference the mapframe and its associated map
      MapFrame mf = layout.FindElement("Map Frame") as MapFrame;
      Map m = mf.Map;
    
      //Reference a feature layer and build a query (to return a single feature)
      FeatureLayer fl = m.FindLayers("GreatLakes").First() as FeatureLayer;
      QueryFilter qf = new QueryFilter();
      string whereClause = "NAME = 'Lake Erie'";
      qf.WhereClause = whereClause;
    
      //Zoom to the feature
      using (ArcGIS.Core.Data.RowCursor rowCursor = fl.Search(qf))
      {
        while (rowCursor.MoveNext())
        {
          //Get the shape from the row and set extent
          using (var feature = rowCursor.Current as ArcGIS.Core.Data.Feature)
          {
            Polygon polygon = feature.GetShape() as Polygon;
            Envelope env = polygon.Extent as Envelope;
            mf.SetCamera(env);
    
            //Zoom out 15 percent
            Camera cam = mf.Camera;
            cam.Scale = cam.Scale * 1.15;
            mf.SetCamera(cam);
          }
        }
      }
    });
    Find a layer
    //Finds layers by name and returns a read only list of Layers
    IReadOnlyList<Layer> layers = aMap.FindLayers("cities", true);
    
    //Finds a layer using a URI.
    //The Layer URI you pass in helps you search for a specific layer in a map
    var lyrFindLayer = MapView.Active.Map.FindLayer("CIMPATH=map/u_s__states__generalized_.xml");
    
    //This returns a collection of layers of the "name" specified. You can use any Linq expression to query the collection.  
    var lyrExists = MapView.Active.Map.GetLayersAsFlattenedList()
                       .OfType<FeatureLayer>().Any(f => f.Name == "U.S. States (Generalized)");
    Requirements

    Target Platforms: Windows 11, Windows 10, Windows 8.1

    See Also