ArcGIS API for Silverlight - Library Reference
LayerDefinitions Property
See Also  Example Send comments on this topic
ESRI.ArcGIS.Client Namespace > ArcGISDynamicMapServiceLayer Class : LayerDefinitions Property

An ObservableCollection of LayerDefinition objects that allow filtering of features for individual layers in an ArcGISDynamicMapServiceLayer.

Syntax

Visual Basic (Declaration) 
Public Property LayerDefinitions As ObservableCollection(Of LayerDefinition)
C# 
public ObservableCollection<LayerDefinition> LayerDefinitions {get; set;}

Remarks

Definition expressions for particular sub-layer LayerID in the LayerDefinition class that are currently not visible will be ignored by the server.

When the LayerDefinitions Property is not set, all features are returned.

A LayerDefinition of 1=1 returns all features.

For a LayerDefinition that contains an apostrophe embedeed within an esriFieldTypeString, escape the apostrophe with a second apostrophe. Example: Name = 'Billy ''O Donald'.

For a LayerDefinition that is based upon a numeric value (i.e. esriFieldTypeDouble, esriFieldTypeInt, etc.) when performing a 'less than' (i.e. the < character) operation in XAML, make sure and escape the syntax with the characters &lt;. Example: Cost &lt; 100.

For ArcGISDynamicMapServiceLayers that are served up by ArcGIS Server 10 and above, it is possible to use Time as part of a DATE LayerDefintion. Example: Birthday > DATE '1/15/2008 18:30:00' or Observation1 = DATE '12/27/2008 03:15:00'. For ArcGISDynamicMapServiceLayers that are served up by ArcGIS 9.31 and earlier, Time portions of DATE LayerDefinitions are not supported. Use the following syntax instead: Birthday > DATE '1/15/2008' or Observation1 = DATE '12/27/2008'.

TIP: One way to speed up development time in determining the correct LayerDefinition syntax for the ObservableCollections(Of LayerDefinitions) is to use Query functionality for the web service in a web browser. The way you do this is via the following steps:

1. Copy the ArcGISDynamicMapServiceLayer.URL property into the address bar of the web browser and then click on the hyperlink for the specific sub-layer LayerID you want to perform a Query on. See the following screen shot:

Use the ArcGISDynamicMapServiceLayer.URL property in the address bar of the web browser.

2. For the specific ArcGISDynamicMapServiceLayer, scroll to the bottom of the web page and click on the Query hyperlink. See the following screen shot:

Click on the Query hyperlink in the web browser.

3. Specify the LayerDefinition.Definition syntax you want to test in the Where textbox and click either of the Query buttons. Look at the bottom of the page to see if any records have been returned (# results) to know if your LayerDefintion.Definition syntax is correct. See the following screen shot:

Use the Where textbox to specify various syntaxes to seehow many records are returned.

Example

How to use:

When the application loads, choose a specific LayerDefinition syntax in the ListBox and then click the Button. The Map will redraw with the LayerDefinitions applied to the ArcGISDynamicMapServiceLayer. A count of the features returned will also be displayed.

The XAML code in this example is used in conjunction with the code-behind (C# or VB.NET) to demonstrate the functionality.

The following screen shot corresponds to the code example in this page.

Choosing from various LayerDefinition values in a ListBox to re-render the ArcGISDynamicMapServiceLayer.

The following screenshot provides a collage of all the various LayerDefinitions options for the ArcGISDynamicMapServiceLayer showing earthquakes since 1970.

Example LayerDefinitions and their results.

XAMLCopy Code
<Grid x:Name="LayoutRoot">
    
  <!-- Add a Map Control. -->
  <esri:Map Background="White" Name="Map1" WrapAround="True" Margin="12,123,0,0" HorizontalAlignment="Left" 
            VerticalAlignment="Top" Height="283" Width="578">
     
    <!-- Add a background ArcGISDynamicMapServiceLayer. -->
    <esri:ArcGISDynamicMapServiceLayer ID="ESRI_StreetMap_World_2D" 
          Url="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" />
    
    <!-- 
    Add another ArcGISDynamicMapServiceLayer. This will be the one that has various LayerDefinitions
    set. By default no LayerDefinitions are set unless explicitly set on the server. Note how the
    Initialized Event is wired up so that code-behind can be utilized.
    -->
    <esri:ArcGISDynamicMapServiceLayer ID="Earthquakes" Initialized="ArcGISDynamicMapServiceLayer_Initialized"
    Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer" />
       
     
    <!-- 
    ===================================================================================
    The following are some XAML examples of various ArcGISDynamicLayer.LayerDefinitions 
    NOTE: LayerID="0" is the Earthquakes1970 sub-layer: 
    ===================================================================================
    -->
    
    <!-- 
    The Definition only displays earthquakes that have a Magnitude greater than 6. The field 
    Magnitude is of type esriFieldTypeDouble. 
    -->
    <!--
    <esri:ArcGISDynamicMapServiceLayer ID="Earthquakes"
              Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer">
      <esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
        <esri:LayerDefinition LayerID="0" Definition="Magnitude > 6" />
      </esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
    </esri:ArcGISDynamicMapServiceLayer>
    -->
    
    <!-- 
    The Definition only displays earthquakes that have a Magnitude greater than 3 and less than 6. 
    The field Magnitude is of type esriFieldTypeDouble. 
    -->
    <!--
    <esri:ArcGISDynamicMapServiceLayer ID="Earthquakes"
              Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer">
      <esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
        <esri:LayerDefinition LayerID="0" Definition="Magnitude > 3 AND Magnitude &lt; 6" />
      </esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
    </esri:ArcGISDynamicMapServiceLayer>
    -->
    
    <!-- 
    The Definition only displays earthquake events where the Name of the earthquake contains the 
    letters 'CHINA'. Note: the Definition is case sensitive. The field Name is of type esriFieldTypeString.
    -->
    <!--
    <esri:ArcGISDynamicMapServiceLayer ID="Earthquakes"
          Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer">
      <esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
        <esri:LayerDefinition LayerID="0" Definition="Name LIKE 'CHINA'" />
      </esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
    </esri:ArcGISDynamicMapServiceLayer>
    -->
    
    <!-- 
    The Definition only displays earthquakes where the Name of the earthquake exactly matches the 
    letters 'VENEZUELA'. Note: the Definition is case sensitive. The field Name is of type esriFieldTypeString. 
    -->
    <!--
    <esri:ArcGISDynamicMapServiceLayer ID="Earthquakes"
          Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer">
      <esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
        <esri:LayerDefinition LayerID="0" Definition="Name = 'VENEZUELA'" />
      </esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
    </esri:ArcGISDynamicMapServiceLayer>
    -->
    
    <!-- 
    The Definition only displays earthquakes if they occured after January 15, 2000. The field Date_ is 
    of type esriFieldTypeDate. 
    -->
    <!--
    <esri:ArcGISDynamicMapServiceLayer ID="Earthquakes"
          Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer">
      <esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
        <esri:LayerDefinition LayerID="0" Definition="Date_ > DATE '1/15/2000'" />
      </esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
    </esri:ArcGISDynamicMapServiceLayer>
    -->
    
  </esri:Map>
    
  <!-- Add controls for the user to interact with and see the effect of choosing different syntaxes for setting
  the ArcGISDynamicMapServiceLayer.LayerDefinitions. -->
  <ListBox Height="143" HorizontalAlignment="Left" Margin="12,421,0,0" Name="ListBox_LayerDefinitions" 
           VerticalAlignment="Top" Width="380" />
  <sdk:Label Height="28" Margin="398,531,301,0" Name="Label1" VerticalAlignment="Top" Content="Records Returned:"/>
  <TextBlock Height="23" HorizontalAlignment="Left" Margin="505,529,0,0" Name="TextBlock_RecordsReturned" 
             VerticalAlignment="Top" Width="85" />
  <Button Content="Apply LayerDefinitions" Height="105" HorizontalAlignment="Left" Margin="398,418,0,0" 
          Name="Button_LayerDefinitions" VerticalAlignment="Top" Width="192" Click="Button_LayerDefinitions_Click"/>
  
  <!-- Provide the instructions on how to use the sample code. -->
  <TextBlock Height="107" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="590" 
             TextWrapping="Wrap" Text="When the application loads, choose a specific LayerDefinition syntax in
             the ListBox and then click the Button. The Map will redraw with the LayerDefinitions applied to the
             ArcGISDynamicMapServiceLayer. A count of the features returned will also be displayed."/>
</Grid>
C#Copy Code
private void ArcGISDynamicMapServiceLayer_Initialized(object sender, System.EventArgs e)
{
  // When the ArcGISDynamicMapServiceLayer Initializes add several strings that will be used for the
  // LayerDefinition syntax into the ListBox.
  
  ListBox_LayerDefinitions.Items.Add("[NO DEFINITION SET]");
  ListBox_LayerDefinitions.Items.Add("Magnitude > 6");
  ListBox_LayerDefinitions.Items.Add("Magnitude > 3 AND Magnitude < 6");
  ListBox_LayerDefinitions.Items.Add("Name LIKE 'CHINA'");  
  ListBox_LayerDefinitions.Items.Add("Name = 'VENEZUELA'");
  ListBox_LayerDefinitions.Items.Add("Date_ > DATE '1/15/2000'");
  
  // Choose the first string in the ListBox.
  ListBox_LayerDefinitions.SelectedIndex = 0;
}
            
private void Button_LayerDefinitions_Click(object sender, System.Windows.RoutedEventArgs e)
{
  // Get the second layer in the LayerInfo collection. Cast it to an ArcGISDynamicMapServiceLayer. 
  ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = (ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)(Map1.Layers[1]);
  
  // Set the .LayerDefinition for the .LayerID = 0 (the Earthquakes1970 sub-layer). By default no LayerDefinition 
  // is set unless explicitly set on the server.
  ESRI.ArcGIS.Client.LayerDefinition myDefinition = new ESRI.ArcGIS.Client.LayerDefinition();
  myDefinition.LayerID = 0;
  
  // Get the string that will be used for the ArcGISDynamicMapServiceLayer.LayerDefinitions Property from the ListBox.
  string myUserChoice = ListBox_LayerDefinitions.SelectedItem.ToString();
  
  // Set theUserChoice string to Nothing/null if they choose the first option in the ListBox. NOTE: This will cause 
  // ArcGIS Server to return all features for the ArcGISDynamicMapServiceLayer.
  if (myUserChoice == "[NO DEFINITION SET]")
  {
    myUserChoice = null;
  }
  
  // Set the ArcGISDynamicMapServiceLayer.Definition to the string the user chose in the ListBox.
  myDefinition.Definition = myUserChoice;
  
  // Create an ObservableCollection and add the .Definition to it.
  System.Collections.ObjectModel.ObservableCollection<ESRI.ArcGIS.Client.LayerDefinition> myObservableCollection = new System.Collections.ObjectModel.ObservableCollection<ESRI.ArcGIS.Client.LayerDefinition>();
  myObservableCollection.Add(myDefinition);
  
  // Apply the custom LayerDefinition to the LayerDefinitions ObservableCollection.
  myArcGISDynamicMapServiceLayer.LayerDefinitions = myObservableCollection;
  
  // Refresh the layer to apply the new LayerDefinitions.
  myArcGISDynamicMapServiceLayer.Refresh();
  
  // Call the function that will display the number of features returned from applied LayerDefintions.
  GetFeatureCount(myUserChoice);
}
            
private void GetFeatureCount(string sqlQuery)
{
  // This function approximates the same SQL syntax that is used by  the ArcGISDynamicMapServiceLayer's 
  // LayerDefintions Property and displays the feature count back to the user.
  
  // Get the ArcGISDynamicMapServicelayer.
  ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = null;
  myArcGISDynamicMapServiceLayer = (ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)(Map1.Layers["Earthquakes"]);
  
  // Get the Url of the ArcGISDynamicMapServiceLayer. 
  string myUrl = myArcGISDynamicMapServiceLayer.Url;
  
  // Get the ID of the Earthquakes1970 sub-layer.
  int mySubLayerID = myArcGISDynamicMapServiceLayer.Layers[0].ID;
  
  // Create a Query. Use the Map's Extent and SpatialReference. Return all the fields. 
  ESRI.ArcGIS.Client.Tasks.Query myQuery = new ESRI.ArcGIS.Client.Tasks.Query();
  myQuery.Geometry = Map1.Extent;
  myQuery.OutSpatialReference = Map1.SpatialReference;
  myQuery.OutFields.Add("*");
  myQuery.Where = sqlQuery;
  
  // Create a QueryTask using the correct Url and Query Parameters.
  ESRI.ArcGIS.Client.Tasks.QueryTask myQueryTask = new ESRI.ArcGIS.Client.Tasks.QueryTask();
  myQueryTask.Url = myUrl + "/" + mySubLayerID.ToString();
  myQueryTask.ExecuteAsync(myQuery);
  
  // Add the Event Handler for the Asynchronous QueryTask request.
  myQueryTask.ExecuteCompleted += myQueryTask_ExecuteCompleted;
}
            
private void myQueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs e)
{
  // Check to ensure we have valid return results.
  if (e.FeatureSet == null)
  {
    return;
  }
  
  // Obtain a FeatureSet from the queryArgs returned from the web service.
  ESRI.ArcGIS.Client.Tasks.FeatureSet myFeatureSet = e.FeatureSet;
  
  // Get the Features from the FeatureSet.
  System.Collections.Generic.IList<ESRI.ArcGIS.Client.Graphic> myFeatures = myFeatureSet.Features;
  
  // Display the number of features returned from the QueryTask.
  int myCount = myFeatures.Count;
  TextBlock_RecordsReturned.Text = myCount.ToString();
}
VB.NETCopy Code
Private Sub ArcGISDynamicMapServiceLayer_Initialized(sender As System.Object, e As System.EventArgs)
  
  ' When the ArcGISDynamicMapServiceLayer Initializes add several strings that will be used for the
  ' LayerDefinition syntax into the ListBox.
  
  ListBox_LayerDefinitions.Items.Add("[NO DEFINITION SET]")
  ListBox_LayerDefinitions.Items.Add("Magnitude > 6")
  ListBox_LayerDefinitions.Items.Add("Magnitude > 3 AND Magnitude < 6")
  ListBox_LayerDefinitions.Items.Add("Name LIKE 'CHINA'") 
  ListBox_LayerDefinitions.Items.Add("Name = 'VENEZUELA'")
  ListBox_LayerDefinitions.Items.Add("Date_ > DATE '1/15/2000'")
  
  ' Choose the first string in the ListBox.
  ListBox_LayerDefinitions.SelectedIndex = 0
  
End Sub
            
Private Sub Button_LayerDefinitions_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
  
  ' Get the second layer in the LayerInfo collection. Cast it to an ArcGISDynamicMapServiceLayer. 
  Dim myArcGISDynamicMapServiceLayer As ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer = CType(Map1.Layers.Item(1), ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)
  
  ' Set the .LayerDefinition for the .LayerID = 0 (the Earthquakes1970 sub-layer). By default no LayerDefinition 
  ' is set unless explicitly set on the server.
  Dim myDefinition As New ESRI.ArcGIS.Client.LayerDefinition
  myDefinition.LayerID = 0
  
  ' Get the string that will be used for the ArcGISDynamicMapServiceLayer.LayerDefinitions Property from the ListBox.
  Dim myUserChoice As String = ListBox_LayerDefinitions.SelectedItem.ToString
  
  ' Set theUserChoice string to Nothing/null if they choose the first option in the ListBox. NOTE: This will cause 
  ' ArcGIS Server to return all features for the ArcGISDynamicMapServiceLayer.
  If myUserChoice = "[NO DEFINITION SET]" Then
    myUserChoice = Nothing
  End If
  
  ' Set the ArcGISDynamicMapServiceLayer.Definition to the string the user chose in the ListBox.
  myDefinition.Definition = myUserChoice
  
  ' Create an ObservableCollection and add the .Definition to it.
  Dim myObservableCollection As New System.Collections.ObjectModel.ObservableCollection(Of ESRI.ArcGIS.Client.LayerDefinition)
  myObservableCollection.Add(myDefinition)
  
  ' Apply the custom LayerDefinition to the LayerDefinitions ObservableCollection.
  myArcGISDynamicMapServiceLayer.LayerDefinitions = myObservableCollection
  
  ' Refresh the layer to apply the new LayerDefinitions.
  myArcGISDynamicMapServiceLayer.Refresh()
  
  ' Call the function that will display the number of features returned from applied LayerDefintions.
  GetFeatureCount(myUserChoice)
  
End Sub
            
Private Sub GetFeatureCount(sqlQuery As String)
  
  ' This function approximates the same SQL syntax that is used by  the ArcGISDynamicMapServiceLayer's 
  ' LayerDefintions Property and displays the feature count back to the user.
  
  ' Get the ArcGISDynamicMapServicelayer.
  Dim myArcGISDynamicMapServiceLayer As ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer
  myArcGISDynamicMapServiceLayer = CType(Map1.Layers("Earthquakes"), ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer)
  
  ' Get the Url of the ArcGISDynamicMapServiceLayer. 
  Dim myUrl As String = myArcGISDynamicMapServiceLayer.Url
  
  ' Get the ID of the Earthquakes1970 sub-layer.
  Dim mySubLayerID As Integer = myArcGISDynamicMapServiceLayer.Layers(0).ID
  
  ' Create a Query. Use the Map's Extent and SpatialReference. Return all the fields. 
  Dim myQuery As New ESRI.ArcGIS.Client.Tasks.Query
  myQuery.Geometry = Map1.Extent
  myQuery.OutSpatialReference = Map1.SpatialReference
  myQuery.OutFields.Add("*")
  myQuery.Where = sqlQuery
  
  ' Create a QueryTask using the correct Url and Query Parameters.
  Dim myQueryTask As New ESRI.ArcGIS.Client.Tasks.QueryTask
  myQueryTask.Url = myUrl + "/" + mySubLayerID.ToString
  myQueryTask.ExecuteAsync(myQuery)
  
  ' Add the Event Handler for the Asynchronous QueryTask request.
  AddHandler myQueryTask.ExecuteCompleted, AddressOf myQueryTask_ExecuteCompleted
  
End Sub
            
Private Sub myQueryTask_ExecuteCompleted(sender As Object, e As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)
  
  ' Check to ensure we have valid return results.
  If e.FeatureSet Is Nothing Then
    Return
  End If
  
  ' Obtain a FeatureSet from the queryArgs returned from the web service.
  Dim myFeatureSet As ESRI.ArcGIS.Client.Tasks.FeatureSet = e.FeatureSet
  
  ' Get the Features from the FeatureSet.
  Dim myFeatures As System.Collections.Generic.IList(Of ESRI.ArcGIS.Client.Graphic) = myFeatureSet.Features
  
  ' Display the number of features returned from the QueryTask.
  Dim myCount As Integer = myFeatures.Count
  TextBlock_RecordsReturned.Text = myCount.ToString
  
End Sub

Requirements

Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7

See Also

© ESRI, Inc. All Rights Reserved.