Feature Layer Edit Attribute

Download Samples Repository

Description

Demonstrates how to update feature attributes in feature layer.

"Desktop" "Store" "Phone" Available for Desktop, Store, Phone

Sample Code

<UserControl x:Class="ArcGISRuntime.Samples.Desktop.FeatureLayerEditAttribute"
			 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
			 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
			 xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">
	<Grid>
		<esri:MapView x:Name="MyMapView"
					  MapViewTapped="MyMapView_MapViewTapped">
			<esri:Map InitialViewpoint="-13629664.9319909,4549128.41263531,-13628885.4336415,4549541.25215971">
				<esri:ArcGISTiledMapServiceLayer ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
				<!-- Makes all features at current extent available for editing -->
				<esri:FeatureLayer ID="Incidents">
					<esri:ServiceFeatureTable ServiceUri="http://sampleserver6.arcgisonline.com/arcgis/rest/services/SF311/FeatureServer/0"
											  OutFields="req_type"/>
				</esri:FeatureLayer>
			</esri:Map>
		</esri:MapView>
		<!-- Used for attribute editing -->
		<Border x:Name="AttributeEditor"
				Visibility="Collapsed"
				Background="White"
				BorderBrush="Black"
				BorderThickness="1"
				HorizontalAlignment="Right"
				VerticalAlignment="Top"
				Margin="30"
				Padding="20">
			<Border.Effect>
				<DropShadowEffect />
			</Border.Effect>
				<StackPanel Orientation="Horizontal">
					<TextBlock Margin="2"
							   VerticalAlignment="Center"
						   Text="Request Type" />
				<ComboBox x:Name="ChoiceList"
							  Margin="2">
				</ComboBox>
				</StackPanel>
		</Border>
		<Border Background="White"
				BorderBrush="Black"
				BorderThickness="1"
				HorizontalAlignment="Left"
				VerticalAlignment="Top"
				Margin="30"
				Padding="20">
			<Border.Effect>
				<DropShadowEffect />
			</Border.Effect>
			<TextBlock Text="Click on a feature to select and edit its attribute."
					   Width="200"
					   TextAlignment="Left"
					   Margin="30,20,20,30"
					   TextWrapping="Wrap" />
		</Border>
	</Grid>
</UserControl>
using Esri.ArcGISRuntime.Controls;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Layers;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace ArcGISRuntime.Samples.Desktop
{
	/// <summary>
	/// Demonstrates how to update feature attributes in feature layer.
	/// </summary>
	/// <title>Feature Layer Edit Attribute</title>
	/// <category>Editing</category>
	public partial class FeatureLayerEditAttribute : UserControl
	{
		// Used to populate choice list for editing field.
		private IEnumerable<KeyValuePair<object, string>> choices;

		public FeatureLayerEditAttribute()
		{
			InitializeComponent();
		}
		
		/// <summary>
		/// Selects feature for editing.
		/// </summary>
		private async void MyMapView_MapViewTapped(object sender, MapViewInputEventArgs e)
		{
			var layer = MyMapView.Map.Layers["Incidents"] as FeatureLayer;
			layer.ClearSelection();
			SetAttributeEditor();
			string message = null;
			try
			{
				// Performs hit test on layer to select feature.
				var features = await layer.HitTestAsync(MyMapView, e.Position);
				if (features == null || !features.Any())
					return;
				var featureID = features.FirstOrDefault();
				layer.SelectFeatures(new long[] { featureID });
				var feature = await layer.FeatureTable.QueryAsync(featureID);
				SetAttributeEditor(feature);
			}
			catch (Exception ex)
			{
				message = ex.Message;
			}
			if (!string.IsNullOrWhiteSpace(message))
				MessageBox.Show(message);
		}

		/// <summary>
		/// Returns choice list for attribute editing.
		/// </summary>
		private IEnumerable<KeyValuePair<object, string>> GetChoices()
		{
			var layer = MyMapView.Map.Layers["Incidents"] as FeatureLayer;
			var table = (ArcGISFeatureTable)layer.FeatureTable;
			// Gets service metadata from table and extract type field information for attribute editing.            
			// Since req_type is also the type id field and no domain was specified in the service, use Types.
			if (!string.IsNullOrWhiteSpace(table.ServiceInfo.TypeIdField) && table.ServiceInfo.Types != null)
			{
				return  from t in table.ServiceInfo.Types
						select new KeyValuePair<object, string>(t.ID, t.Name);
			}
			return null;
		}

		/// <summary>        
		/// Prepares AttributeEditor for editing.
		/// </summary>
		private void SetAttributeEditor(Feature feature = null)
		{
			if (choices == null)
				choices = GetChoices();
			if (ChoiceList.ItemsSource == null && choices != null)
				ChoiceList.ItemsSource = from item in choices
										 select item.Key;
			AttributeEditor.Tag = feature;
			AttributeEditor.Visibility = feature == null ? Visibility.Collapsed : Visibility.Visible;
			if (feature != null)
			{
				var reqType = Convert.ToString(feature.Attributes["req_type"], CultureInfo.InvariantCulture);
				if (!string.IsNullOrWhiteSpace(reqType) && choices != null)
				{
					var selected = choices.FirstOrDefault(item => string.Equals(Convert.ToString(item.Key), reqType)).Key;
					ChoiceList.SelectedItem = selected;
				}
				ChoiceList.SelectionChanged += ChoiceList_SelectionChanged;
			}
		}
				
		/// <summary>
		/// Enables attribute editing and submits attribute edit back to the server.
		/// </summary>
		private async void ChoiceList_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			ChoiceList.SelectionChanged -= ChoiceList_SelectionChanged;
			var feature = (GeodatabaseFeature)AttributeEditor.Tag;
			var layer = MyMapView.Map.Layers["Incidents"] as FeatureLayer;
			var table = (ArcGISFeatureTable)layer.FeatureTable;
			string message = null;
			try
			{
				var item = (string)ChoiceList.SelectedItem;
				feature.Attributes["req_type"] = item;
				await table.UpdateAsync(feature);
				if (table.HasEdits)
				{
					if (table is ServiceFeatureTable)
					{
						var serviceTable = (ServiceFeatureTable)table;
						// Pushes attribute edits back to the server.
						var result = await serviceTable.ApplyEditsAsync();
						if (result.UpdateResults == null || result.UpdateResults.Count < 1)
							return;
						var updateResult = result.UpdateResults[0];
						if (updateResult.Error != null)
							message = updateResult.Error.Message;
					}
				}
			}
			catch (Exception ex)
			{
				message = ex.Message;
			}
			finally
			{
				layer.ClearSelection();
				SetAttributeEditor();
			}
			if (!string.IsNullOrWhiteSpace(message))
				MessageBox.Show(message);
		}
	}
}
Feedback on this topic?