Click or drag to resize
Code Example - QueryTableDataSource

Demonstrates adding a Dynamic Layer via code-behind using a QueryTableDataSource.

Code Example
Query Table Data Source

This section contains selected code files from a Visual Studio project that emphasize specific ArcGIS Runtime SDK (Windows Desktop) features. For example: some code examples may accomplish the bulk of the work as a configuration property in the .xaml file and hence only the .xaml file will be shown in detail below. In other code examples, the .xaml is used to define the configuration of graphical elements for the application but the application logic is performed in the code behind, hence you may see both the .xaml and .cs/.vb files shown in detail below.

<Window x:Class="QueryTableDataSource.MainWindow"
        Title="MainWindow" Height="600" Width="800">

    <Grid x:Name="LayoutRoot">

        <!-- Add a MapView Control. -->
        <esri:MapView x:Name="MapView1" Background="White" HorizontalAlignment="Left" Margin="10,107,0,0" 
                      VerticalAlignment="Top" Height="447" Width="772">

            <!-- Add a Map. -->
            <esri:Map x:Name="Map1">

                <!--Add an ArcGISTiledMapServiceLayer as a backdrop.-->
                <esri:ArcGISDynamicMapServiceLayer ID="myArcGISDynamicMapServiceLayer" 
                    ServiceUri="" />


        <!-- Button to add a Dynamic Layer (using QueryDataSource) via code-behind. -->
        <Button Content="Add a QueryTableDataSource based Dynamic Layer" Height="23" Click="Button1_Click"
          HorizontalAlignment="Left" Margin="10,79,0,0" Name="Button1" VerticalAlignment="Top" Width="772" />

        <!-- Add a TextBlock to hold the instructions on how to use the sample code. -->
        <TextBlock Height="74" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="772" 
            TextWrapping="Wrap" Margin="10,0,0,0" />



SPECIAL NOTE: The XAML displayed above comes from a C# project. If you are a VB.NET developer, you will need to modify the text for the x:Class namespace from "QueryTableDataSource.MainWindow" to be just "MainWindow".

namespace QueryTableDataSource
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : System.Windows.Window
        public MainWindow()

            // Add the instructions on how to use this example code to the TextBlock.
            TextBlock1.Text = "Click the button to add a Dynamic Layer (the state of Kentucky) via code-behind using " + 
                "a QueryTableDataSource. The 'WorkspaceID' and 'DataSourceName' for each Dynamic Layer must be provided to the " + 
                "developer by Manager/Administrator of the ArcGISDynamicMapServiceLayer. Examples of the 'WorkspaceID' and" +
                "'DataSourceName' are provided as comments in code-behind for a public ArcGIS Server that has Dynamic Layers " + 
                "enabled via the LayerDataSource object.";

            // Zoom to the East-Central United States.
            Esri.ArcGISRuntime.Geometry.Envelope myEnvelope = new Esri.ArcGISRuntime.Geometry.Envelope(-10349730, 3871622, -8642421, 5066738);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myEnvelope);

        private void Button1_Click(object sender, System.Windows.RoutedEventArgs e)

            // Add a Layer on-the-fly based on an ArcGISDynamicMapServiceLayer that has Dynamic Layer capabilities enabled.
            // The Dynamic Layer is being generated uses the 'Workspace Type' of 'Database' via the LayerDataSource object. Review the
            // ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer Class documentation in the API reference for a full discussion.
            // In order to create this Dynamic Layer, information was shared between the Manager/Administrator of the
            // ArcGISDynamicMapServiceLayer ArcGIS Server service and the application developer.
            // The Url for the ArcGISDynamicMapServiceLayer is: 
            // ""
            // The WorkspaceID used in the LayerDataSource for the Dynamic Layer is: "MyDatabaseWorkspaceIDSSR2"
            // There are several DataSourceName types associated with the WorkspaceID that could be used to create the Rendering 
            // of a Dynamic Layer. Think of the DataSourceName as the string name of the layer that will be drawn dynamically.
            // A listing of the available DataSourceNames (and their geometry type) are:
            // "ss6.gdb.states" <== Polygons
            // "ss6.gdb.cities" <== Points
            // "ss6.gdb.counties" <== Polygons
            // "ss6.gdb.highways" <== Polylines
            // "ss6.gdb.lakes" <== Polygons
            // "ss6.gdb.urbanareas" <== Polygons
            // "ss6.gdb.places" <== Points
            // "ss6.gdb.rivers" <== Polylines
            // "ss6.gdb.railroads" <== Polylines
            // NOTE: If you use this code and change to a different Dynamic Layer type make sure and change:
            // (1) the myQueryDataSource.GeometryType to the correct geometry (i.e. Point, Polyline, etc.)
            // (2) modify the symbol to be the correct type (i.e. SimpleLineSymbol, SimpleMarkerSymbol, etc.)
            // (3) the QueryTableDataSource.Query to the correct SQL select statement

            // Create a QueryDataSource object and set all of it's Properties to valid values.
            Esri.ArcGISRuntime.Layers.QueryTableDataSource myQueryDataSource = new Esri.ArcGISRuntime.Layers.QueryTableDataSource();
            myQueryDataSource.GeometryType = Esri.ArcGISRuntime.Geometry.GeometryType.Polygon;
            string[] myOIDFields = { "OBJECTID" };
            myQueryDataSource.OIdFields = myOIDFields;
            myQueryDataSource.Query = "SELECT * FROM ss6.gdb.states where state_name = 'Kentucky'";
            Esri.ArcGISRuntime.Geometry.SpatialReference mySpatialReference = new Esri.ArcGISRuntime.Geometry.SpatialReference(4326);
            myQueryDataSource.SpatialReference = mySpatialReference;
            myQueryDataSource.WorkspaceID = "MyDatabaseWorkspaceIDSSR2";

            // Create a new LayerDataSource and set its DataSource Property to the QueryDataSource.
            Esri.ArcGISRuntime.Layers.LayerDataSource myLayerDataSource = new Esri.ArcGISRuntime.Layers.LayerDataSource();
            myLayerDataSource.DataSource = myQueryDataSource;

            // Create a new DynamicLayerInfo object and set its ID and Source Properties.
            Esri.ArcGISRuntime.Layers.DynamicLayerInfo myDynamicLayerInfo = new Esri.ArcGISRuntime.Layers.DynamicLayerInfo();
            myDynamicLayerInfo.ID = 102; // Must be the same as the LayerDrawingOptions.LayerID
            myDynamicLayerInfo.Source = myLayerDataSource;

            // Create a new DynamicLayerInfoCollection and add the DynamicLayerInfo object into it.
            Esri.ArcGISRuntime.Layers.DynamicLayerInfoCollection myDynamicLayerInfoCollection = new Esri.ArcGISRuntime.Layers.DynamicLayerInfoCollection();

            // Create a SimpleFillSymbol (Green Fill, with Black Border, and BorderThickness of 2).
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol.Color = System.Windows.Media.Colors.Green;
            Esri.ArcGISRuntime.Symbology.SimpleLineSymbol mySimpleLineSymbol = new Esri.ArcGISRuntime.Symbology.SimpleLineSymbol();
            mySimpleLineSymbol.Color = System.Windows.Media.Colors.Black;
            mySimpleFillSymbol.Outline = mySimpleLineSymbol;

            // Create a new SimpleRenderer based upon the SimpleFillSymbol.
            Esri.ArcGISRuntime.Symbology.SimpleRenderer mySimpleRenderer = new Esri.ArcGISRuntime.Symbology.SimpleRenderer();
            mySimpleRenderer.Symbol = mySimpleFillSymbol;

            // Create a new LayerDrawingOptions object which is key to applying our custom Rendering of the Dynamic Layer.
            // It is imperative that the LayerDrawingOptions.LayerID = DynamicLayerInfo.ID so that the Dynamic Layer draws
            // using the new symbology.
            Esri.ArcGISRuntime.Layers.LayerDrawingOptions myLayerDrawingOptions = new Esri.ArcGISRuntime.Layers.LayerDrawingOptions();
            myLayerDrawingOptions.LayerID = 102; // Must be the same as the DynammicLayerInfo.ID
            myLayerDrawingOptions.Renderer = mySimpleRenderer;

            // Create a new LayerDrawinOptionsCollection and add the LayerDraingOptions object into it.
            Esri.ArcGISRuntime.Layers.LayerDrawingOptionCollection myLayerDrawingOptionsCollection = new Esri.ArcGISRuntime.Layers.LayerDrawingOptionCollection();

            // Create a new Dynamic Layer that is based upon on a LayerDataSource of an ArcGISDynamicMapServiceLayer
            // and apply it's custom rendering.
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = new Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer();
            myArcGISDynamicMapServiceLayer.ServiceUri = "";
            myArcGISDynamicMapServiceLayer.DynamicLayerInfos = myDynamicLayerInfoCollection;
            myArcGISDynamicMapServiceLayer.LayerDrawingOptions = myLayerDrawingOptionsCollection;

            // Add the Dynamic Layer to the Map. This causes the round trip server request to occur.