View in MAUI WPF WinUI View on GitHub

Add, delete, and download attachments for features from a service.

Image of edit feature attachments

Use case

Attachments provide a flexible way to manage additional information that is related to your features. Attachments allow you to add files to individual features, including: PDFs, text documents, or any other type of file. For example, if you have a feature representing a building, you could use attachments to add multiple photographs of the building taken from several angles, along with PDF files containing the building’s deed and tax information.

How to use the sample

Tap a feature to load its attachments. Use the buttons to save, delete, or add attachments.

How it works

  1. Create a ServiceFeatureTable from a URL.
  2. Create a FeatureLayer object from the service feature table.
  3. Select features from the feature layer with FeatureLayer.SelectFeatures().
  4. To fetch the feature’s attachments, cast to an ArcGISFeature and useArcGISFeature.GetAttachmentsAsync().
  5. To add an attachment to the selected ArcGISFeature, create an attachment and use ArcGISFeature.AddAttachmentAsync().
  6. To delete an attachment from the selected ArcGISFeature, use the ArcGISFeature.DeleteAttachmentAsync().
  7. After a change, apply the changes to the server using ServiceFeatureTable.ApplyEditsAsync().

Relevant API

  • ApplyEditsAsync
  • DeleteAttachmentAsync
  • FeatureLayer
  • FetchAttachmentsAsync
  • FetchDataAsync
  • ServiceFeatureTable
  • UpdateFeatureAsync

Additional information

Attachments can only be added to and accessed on service feature tables when their HasAttachments property is true.

Tags

data, image, JPEG, PDF, picture, PNG, TXT

Sample Code

EditFeatureAttachments.xaml EditFeatureAttachments.xaml EditFeatureAttachments.xaml.cs
<ContentPage x:Class="ArcGIS.Samples.EditFeatureAttachments.EditFeatureAttachments"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
xmlns:esriUI="clr-namespace:Esri.ArcGISRuntime.Maui;assembly=Esri.ArcGISRuntime.Maui">
<ContentPage.Resources>
<Style x:Key="IconStyle" TargetType="Button">
<Style.Setters>
<Setter Property="FontFamily" Value="CalciteUIIconsMediumFontFamily" />
<Setter Property="FontSize" Value="24" />
<Setter Property="Margin" Value="5" />
</Style.Setters>
</Style>
</ContentPage.Resources>
<Grid Style="{DynamicResource EsriSampleContainer}">
<esriUI:MapView x:Name="MyMapView"
GeoViewTapped="MapView_Tapped"
Style="{DynamicResource EsriSampleGeoView}" />
<Border Style="{DynamicResource EsriSampleControlPanel}">
<StackLayout>
<Label Margin="5"
FontAttributes="Bold"
HorizontalTextAlignment="Center"
Text="Tap features to select. Download, upload, or delete attachments." />
<ListView x:Name="AttachmentsListBox"
HeightRequest="100"
IsEnabled="False">
<!-- ItemTemplate defines how each item (Attachment) is rendered. -->
<ListView.ItemTemplate>
<DataTemplate x:DataType="esri:Attachment">
<ViewCell>
<Grid ColumnDefinitions="*,auto,auto" RowDefinitions="auto">
<Label Margin="5"
Text="{Binding Name}"
VerticalTextAlignment="Center" />
<!-- DataTemplate sets the item as the button's DataContext automatically. -->
<Button Grid.Column="1"
Clicked="DownloadAttachment_Click"
Style="{StaticResource IconStyle}"
Text="{StaticResource CalciteUIIcons_Glyph_Download}"
ToolTipProperties.Text="Download" />
<Button Grid.Column="2"
Clicked="DeleteAttachment_Click"
Style="{StaticResource IconStyle}"
Text="{StaticResource CalciteUIIcons_Glyph_Trash}"
ToolTipProperties.Text="Delete" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Grid ColumnDefinitions="auto,auto">
<Button x:Name="AddAttachmentButton"
Clicked="AddAttachment_Click"
IsEnabled="False"
Style="{StaticResource IconStyle}"
Text="{StaticResource CalciteUIIcons_Glyph_Upload}"
ToolTipProperties.Text="Add attachment" />
<Label Grid.Column="1"
HorizontalOptions="End"
Text="Add attachment"
VerticalOptions="Center" />
</Grid>
</StackLayout>
</Border>
</Grid>
</ContentPage>