Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Feature layer definition expression

This code sample is available for these platforms:
View Sample on GitHub

Limit the features to display on the map using a definition expression.

screenshot

Use case

One use case includes improving application performance by restricting the amount of features returned from a feature service. The definition expression is applied to visualized features as well as the underlying feature table. Another use case is the data in a FeatureLayer may be sensitive and require a certain level of authorization/permission to view. By setting a definition expression to only those records specified the data is protected from unauthorized access.

How to use the sample

Press the 'Apply Expression' button to limit the features requested from the feature layer to those specified by the SQL query definition expression. Click the 'Reset Expression' button to remove the definition expression on the feature layer, which returns all the records.

How it works

  1. Create a service feature table from a URL.
  2. Create a feature layer from the service feature table.
  3. Set the limit of the features on your feature layer using the DefinitionExpression.

Relevant API

  • DefinitionExpression
  • FeatureLayer
  • ServiceFeatureTable

Tags

DefinitionExpression, ServiceFeatureTable, definition expression, filter, limit data, query, restrict data, sql where clause

Sample Code

<UserControl
    x:Class="ArcGISRuntime.UWP.Samples.FeatureLayerDefinitionExpression.FeatureLayerDefinitionExpression"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esriUI="using:Esri.ArcGISRuntime.UI.Controls">
    <Grid>
        <esriUI:MapView x:Name="MyMapView" />
        <Border Style="{StaticResource BorderStyle}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Button Content="Apply expression"
                        Grid.Column="0"
                        HorizontalAlignment="Stretch"
                        Margin="0,0,2.5,0"
                        Click="OnApplyExpressionClicked" />
                <Button Content="Reset expression"
                        Grid.Column="1"
                        HorizontalAlignment="Stretch"
                        Margin="2.5,0,0,0"
                        Click="OnResetButtonClicked" />
            </Grid>
        </Border>
    </Grid>
</UserControl>
// Copyright 2016 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an 
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific 
// language governing permissions and limitations under the License.

using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using System;
using Windows.UI.Xaml;

namespace ArcGISRuntime.UWP.Samples.FeatureLayerDefinitionExpression
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Feature layer definition expression",
        "Layers",
        "This sample demonstrates how to apply definition expression to a feature layer for filtering features. It also shows how to reset the definition expression.",
        "")]
    public partial class FeatureLayerDefinitionExpression
    {
        // Create and hold reference to the feature layer
        private FeatureLayer _featureLayer;

        public FeatureLayerDefinitionExpression()
        {
            InitializeComponent();

            // Setup the control references and execute initialization 
            Initialize();
        }

        private void Initialize()
        {
            // Create new Map with basemap
            Map myMap = new Map(Basemap.CreateTopographic());

            // Create a MapPoint the map should zoom to
            MapPoint mapPoint = new MapPoint(
                -13630484, 4545415, SpatialReferences.WebMercator);

            // Set the initial viewpoint for map
            myMap.InitialViewpoint = new Viewpoint(mapPoint, 90000);

            // Provide used Map to the MapView
            MyMapView.Map = myMap;

            // Create the uri for the feature service
            Uri featureServiceUri = new Uri(
                "https://sampleserver6.arcgisonline.com/arcgis/rest/services/SF311/FeatureServer/0");

            // Initialize feature table using a url to feature server url
            ServiceFeatureTable featureTable = new ServiceFeatureTable(featureServiceUri);

            // Initialize a new feature layer based on the feature table
            _featureLayer = new FeatureLayer(featureTable);

            // Add the feature layer to the map
            myMap.OperationalLayers.Add(_featureLayer);
  
        }

        private void OnApplyExpressionClicked(object sender, RoutedEventArgs e)
        {
            // Adding definition expression to show specific features only
            _featureLayer.DefinitionExpression = "req_Type = 'Tree Maintenance or Damage'";
        }

        private void OnResetButtonClicked(object sender, RoutedEventArgs e)
        {
            // Reset the definition expression to see all features again
            _featureLayer.DefinitionExpression = "";
        }
    }
}