Monday, January 30, 2012

Updating ArcGIS FeatureLayer in Silverlight Application after performing a Query

There was a scenario where I need to display a Featurelayer in ArcGIS Silverlight Map Control and when the user performs a search on specific attributes (in this example date values), the Featurelayer records must be refreshed / updated on the Map Control.

The XAML file content is as follows. This featurelayer tag will be inside the MapControl.
<esri:FeatureLayer ID="Your Layer ID" Url="YourURL" Where="1=1" OutFields="All fields required for query seperated by comma." >
</esri:FeatureLayer>

This Button tag when clicked will invoke the Featurelayer Update Action. Note the use of Interaction Triggers.

<Button Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="buttonSearchDates" Content="Search Dates">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click" PreviewInvoke="EventTrigger_PreviewInvoke">
<esri2009:UpdateFeatureLayerAction
FeatureLayerID="Your Layer ID"
TargetName="MapControl" />
</i:EventTrigger>
</i:Interaction.Triggers>

</Button>


Make sure the namespaces are declared properly like this.

xmlns:esri2009="http://schemas.esri.com/arcgis/client/2009"
xmlns:esriBehaviors="clr-namespace:ESRI.ArcGIS.Client.Behaviors;assembly=ESRI.ArcGIS.Client.Behaviors"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

You also need references to these DLL files. You may need to download Microsoft Expression Blend SDK 4. This coding was done using Silverlight 4.

Microsoft.Expression.Interactions.dll
System.Windows.Interactivity.dll

In the csharp file these are the functions

private void EventTrigger_PreviewInvoke(object sender, System.Windows.Interactivity.PreviewInvokeEventArgs e)
{
PerformQuery(datePickerFrom.SelectedDate.Value, datePickerTo.SelectedDate.Value);
}

private void PerformQuery(DateTime startDate, DateTime endDate)
{
ESRI.ArcGIS.Client.FeatureLayer oFeatureLayer = MapControl.Layers["Your Layer ID"] as ESRI.ArcGIS.Client.FeatureLayer;
if (oFeatureLayer != null)
  {
    string whereClause = GetDateWhereClause(startDate, endDate);
    oFeatureLayer.Where = whereClause;
    oFeatureLayer.Refresh();
   }
}

Hope this will help some programmer.

Cheers
Anand

All Blogs so far ...