ArcGIS Pro 3.7 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / BasicFeatureLayer Class / SetActiveDefinitionQuery Method
The name of the query definition to set as active.
Example

In This Topic
    SetActiveDefinitionQuery Method (BasicFeatureLayer)
    In This Topic
    Sets the definition query matching the specified queryName to be the active definition query. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    Public Sub SetActiveDefinitionQuery( _
       ByVal queryName As String _
    ) 
    public void SetActiveDefinitionQuery( 
       string queryName
    )

    Parameters

    queryName
    The name of the query definition to set as active.
    Exceptions
    ExceptionDescription
    There are no query definitions.
    The specified queryName doesn't match any items in the query definition collection.
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Remarks
    If the queryName is null, then the active query definition is removed and the layer no longer has an active definition query.
    Example
    Querying a feature layer with a spatial filter
    {
        var layers = map.GetLayersAsFlattenedList().OfType<FeatureLayer>();
        var layerToQuery = layers.FirstOrDefault(f => f.Name == "USNationalParks");
        if (layerToQuery == null) return;
    
        string whereClause = "RecreationVisitsTotal > 1000000"; //More than million visitors a year
    
        var spatialDefnLayer = layers.FirstOrDefault(f => f.Name == "AllUSStates");
        if (spatialDefnLayer == null) return;
        try
        {
            if (MapView.Active == null) return;
            //Note: needs to be called on the MCT
            // Set the spatial filter geometry
            //Get the geometry from the selected features in the feature layer
            var spatialClauseGeom = spatialDefnLayer.GetFeatureOutline(MapView.Active, FeatureOutlineType.Selected);
            //Create the definition query
            DefinitionQuery definitionQuery = new DefinitionQuery
            {
                WhereClause = whereClause,
                Name = $"{layerToQuery.Name}"
            };
            //Setting the spatial filter to the Definition Query
            if (definitionQuery.CanSetFilterGeometry(spatialClauseGeom))
            {
                definitionQuery.SetFilterGeometry(spatialClauseGeom);
            }
    
            layerToQuery.InsertDefinitionQuery(definitionQuery);
            layerToQuery.SetActiveDefinitionQuery(definitionQuery.Name);
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine($"Error querying feature layer: {ex.Message}");
        }
    }
    Apply A Definition Query Filter to a Feature Layer
    {
        var us_parks = map.GetLayersAsFlattenedList()
      .OfType<FeatureLayer>().First(l => l.Name == "USNationalParks");
        //Note: needs to be called on the MCT
        var def_query = new DefinitionQuery("CaliforniaParks",
                          "STATE_ABBR = 'CA'");
    
        us_parks.InsertDefinitionQuery(def_query);
        us_parks.SetDefinitionQuery("STATE_ABBR = 'CA'");
    
        //Set it active
        us_parks.SetActiveDefinitionQuery(def_query.Name);
    
        //or....also - set it active when it is inserted
        //us_parks.InsertDefinitionQuery(def_query, true);
    }
    Apply A Definition Query Filter With a Filter Geometry to a Feature Layer
    {
        var greatLakes = map.GetLayersAsFlattenedList()
              .OfType<FeatureLayer>().First(l => l.Name == "Great Lakes");
        var usa_states = map.GetLayersAsFlattenedList()
        .OfType<FeatureLayer>().First(l => l.Name == "US_States");
        //name must be unique
        var def_query = new DefinitionQuery("GreatLakes",
                              "NAME in ('Huron','Michigan','Erie')");
    
        //create a filter geometry - in this example we will use the outline geometry
        //of all visible features from a us states layer...the filter geometry will be
        //intersected with the layer feature geometry when added to the def query
        var filter_geom = usa_states.GetFeatureOutline(mapView, FeatureOutlineType.Visible);
        //other options...
        //var filter_geom = usa_states.GetFeatureOutline(mapView, FeatureOutlineType.All);
        //var filter_geom = usa_states.GetFeatureOutline(mapView, FeatureOutlineType.Selected);
    
        //Geometry must have a valid SR and be point, multi-point, line, or poly
        //Note: needs to be called on the MCT
        if (def_query.CanSetFilterGeometry(filter_geom))
        {
            def_query.SetFilterGeometry(filter_geom);
        }
    
        //Apply the def query
        greatLakes.InsertDefinitionQuery(def_query);
        //Set it active
        greatLakes.SetActiveDefinitionQuery(def_query.Name);
    
        //or....also - set it active when it is inserted
        //greatLakes.InsertDefinitionQuery(def_query, true);
    }
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also