Create features from preset and group shared templates.

Use case
Preset and group shared templates support guided, repeatable, high-quality editing. Preset templates place predefined feature arrangements, while group templates create feature sets relative to a user-defined base geometry. By automatically applying attributes, symbology, geometry settings, and feature relationships/associations, they help field staff create consistent, fully configured assets with only a few choices.
How to use the sample
Hover over a shared template to view its description. Select a template and click on the map to place the geometry. Choose “Complete” to create and add the features locally, or “Cancel” to discard the sketch.
How it works
- Create a map using the URL to a web map.
- Determine the
ISharedTemplateSourceby inspecting map layers, identifyingFeatureLayerinstances backed by aServiceFeatureTable, and retrieving theirServiceGeodatabase. - Call
ISharedTemplateSource.QuerySharedTemplatesAsync()to populate a template picker. Store each template’slayerId, display its name and type, and use native contextual help to show its description. - Call
SharedTemplate.CreateSwatchAsync(layerId)to generate a swatch image for each template, falling back to a default image when a swatch is unavailable. - Call
SharedTemplate.GetDefaultConstructionTool(layerId)to get the template’s default construction method. Use itsGeometryConstructionTool.ToolTypeto choose whether theGeometryEditordraws a point or polyline. - After the user selects “Complete”, call
GeometryEditor.Stop()and use the returned geometry to create features withISharedTemplateSource.CreateFeaturesAsync(sharedTemplate, geometry). Then callISharedTemplateSource.AddFeaturesAsync()to add the resulting feature set to the geodatabase.
Relevant API
- GeometryConstructionTool
- GeometryEditor
- ISharedTemplateSource
- ServiceGeodatabase
- SharedTemplate
- SharedTemplateFeatureCreationSet
- SharedTemplateQueryParameters
About the data
- The sample uses the Parks and Grounds Assets web map.
Tags
edit, feature, group, preset, shared template, shared template source, template
Sample code
// Copyright 2026 Esri.//// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific// language governing permissions and limitations under the License.
#nullable enable
using Esri.ArcGISRuntime.Data;using Esri.ArcGISRuntime.Geometry;using Esri.ArcGISRuntime.Mapping;using Esri.ArcGISRuntime.UI;using Esri.ArcGISRuntime.UI.Editing;using Esri.Calcite.WPF;using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using ImageSource = System.Windows.Media.ImageSource;
namespace ArcGIS.WPF.Samples.AddFeaturesWithSharedTemplate{ [ArcGIS.Samples.Shared.Attributes.Sample( name: "Add features with shared templates", category: "Data", description: "Create features from preset and group shared templates.", instructions: "Hover over a shared template to view its description. Select a template and click on the map to place the geometry. Choose \"Complete\" to create and add the features locally, or \"Cancel\" to discard the sketch.", tags: new[] { "edit", "feature", "group", "preset", "shared template", "shared template source", "template" })] public partial class AddFeaturesWithSharedTemplate { private const string Instruction = "Click on a shared template to create features."; private TemplatePickerItem? _activeTemplateItem = null;
public AddFeaturesWithSharedTemplate() { InitializeComponent();
_ = InitializeAsync(); }
private async Task InitializeAsync() { try { MyMapView.GeometryEditor ??= new GeometryEditor();
// Load a web map that contains a feature service with shared templates. var map = new Map(new Uri("https://www.maps.arcgis.com/home/item.html?id=b635be46dfb545b888077389ac7f0962")); MyMapView.Map = map; await map.LoadAsync();
// Find the shared template source in a service-backed feature layer. ISharedTemplateSource sharedTemplateSource = map.OperationalLayers .OfType<FeatureLayer>() .Select(layer => (layer.FeatureTable as ServiceFeatureTable)?.ServiceGeodatabase) .OfType<ISharedTemplateSource>() .FirstOrDefault() ?? throw new InvalidOperationException("The map does not contain a shared template source.");
// Query without parameters will return all shared templates for all the layers in the map IReadOnlyDictionary<long, IReadOnlyList<SharedTemplate>> templatesByLayer = await sharedTemplateSource.QuerySharedTemplatesAsync(); var templateItems = new List<TemplatePickerItem>();
foreach (KeyValuePair<long, IReadOnlyList<SharedTemplate>> templatesForLayer in templatesByLayer) { if (templateItems.Count == 2) { break; }
foreach (SharedTemplate template in templatesForLayer.Value) { if (templateItems.Count == 2) { break; }
if ((template.Type != SharedTemplateType.Preset && template.Type != SharedTemplateType.Group) || templateItems.Any(item => item.Template.Type == template.Type)) { continue; }
// Create a default swatch image for the template ImageSource imageSource = new CalciteIconImageExtension { Icon = CalciteIcon.AddFeatures, SymbolSize = 36 }.ProvideValue(null!) as ImageSource ?? throw new InvalidOperationException("Unable to create a default swatch image.");
try { // Generate a swatch image for this template RuntimeImage swatch = await template.CreateSwatchAsync(templatesForLayer.Key); imageSource = await swatch.ToImageSourceAsync() ?? imageSource; } catch (Exception) { // Template does not provide a swatch }
templateItems.Add(new TemplatePickerItem(template, templatesForLayer.Key, imageSource)); } }
TemplatePicker.ItemsSource = templateItems; TemplatePicker.Visibility = Visibility.Visible; StatusTextBlock.Text = Instruction; } catch (Exception ex) { UpdateUI("Unable to load templates.", ex); } }
private void UpdateUI(string status, Exception? exception = null) { if (exception is not null) { MessageBox.Show(exception.Message, exception.GetType().Name, MessageBoxButton.OK, MessageBoxImage.Error); }
TemplatePicker.Visibility = Visibility.Visible; MyMapView.GeometryEditor?.Stop(); DrawingButtonsPanel.Visibility = Visibility.Collapsed; StatusTextBlock.Text = $"{status} {Instruction}"; }
private void OnSharedTemplateClicked(object sender, RoutedEventArgs e) { if (sender is not Button button || button.Tag is not TemplatePickerItem templateItem || MyMapView.GeometryEditor is not GeometryEditor geometryEditor || geometryEditor.IsStarted) { return; }
try { TemplatePicker.Visibility = Visibility.Collapsed; DrawingButtonsPanel.Visibility = Visibility.Visible; _activeTemplateItem = templateItem;
GeometryConstructionToolType constructionToolType = _activeTemplateItem.Template.GetDefaultConstructionTool(_activeTemplateItem.LayerId)?.ToolType ?? GeometryConstructionToolType.Unknown;
// Use the construction tool type to choose whether to draw a point or a polyline GeometryType geometryType = constructionToolType switch { GeometryConstructionToolType.Point => GeometryType.Point, GeometryConstructionToolType.Line => GeometryType.Polyline, _ => throw new NotSupportedException($"The {constructionToolType} geometry construction tool is not supported by this sample.") };
StatusTextBlock.Text = geometryType == GeometryType.Point ? "Place a point, then click Complete or Cancel." : "Sketch a line, then click Complete or Cancel.";
geometryEditor.Tool = new VertexTool(); geometryEditor.Start(geometryType); } catch (Exception ex) { UpdateUI("Unable to start drawing.", ex); } }
private async void OnGeometryEditorCompleted(object sender, RoutedEventArgs e) { if (_activeTemplateItem is null || _activeTemplateItem.Template.TemplateSource is not ISharedTemplateSource sharedTemplateSource || MyMapView.GeometryEditor is not GeometryEditor geometryEditor || !geometryEditor.IsStarted) { return; }
Geometry? geometry = geometryEditor.Stop(); DrawingButtonsPanel.Visibility = Visibility.Collapsed;
if (geometry is null || geometry.IsEmpty) { UpdateUI("No geometry was drawn."); return; }
try {
// Creates in-memory features from different layers with default geometry and attributes. SharedTemplateFeatureCreationSet featureCreationSet = await sharedTemplateSource.CreateFeaturesAsync(_activeTemplateItem.Template, geometry);
// Note: You can continue to make attribute changes to this feature creation set.
// Commits the feature creation set to the database. await sharedTemplateSource.AddFeaturesAsync(featureCreationSet);
UpdateUI("Features added."); } catch (Exception ex) { UpdateUI("Unable to create or add features.", ex); } }
private void OnGeometryEditorCanceled(object sender, RoutedEventArgs e) { UpdateUI("Draw canceled."); } }
internal sealed class TemplatePickerItem { internal TemplatePickerItem(SharedTemplate template, long layerId, ImageSource imageSource) { Template = template; LayerId = layerId; ImageSource = imageSource; }
public SharedTemplate Template { get; }
public long LayerId { get; }
public ImageSource ImageSource { get; } }}<UserControl x:Class="ArcGIS.WPF.Samples.AddFeaturesWithSharedTemplate.AddFeaturesWithSharedTemplate" 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" /> <Border MaxWidth="270" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" Style="{StaticResource BorderStyle}"> <StackPanel> <TextBlock x:Name="StatusTextBlock" Text="Loading shared templates..." TextWrapping="Wrap" /> <ItemsControl x:Name="TemplatePicker" Visibility="Collapsed" Margin="0,8,0,0"> <ItemsControl.ItemTemplate> <DataTemplate> <Button HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Click="OnSharedTemplateClicked" Tag="{Binding}" ToolTip="{Binding Template.Description}"> <Grid Margin="0,3"> <Grid.ColumnDefinitions> <ColumnDefinition Width="44" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Image Width="36" Height="36" HorizontalAlignment="Center" VerticalAlignment="Center" Source="{Binding ImageSource}" /> <StackPanel Grid.Column="1" Margin="6,0,0,0" VerticalAlignment="Center"> <TextBlock FontWeight="SemiBold" Text="{Binding Template.Name}" TextWrapping="Wrap" /> <TextBlock Text="{Binding Template.Type, StringFormat=({0})}" /> </StackPanel> </Grid> </Button> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> <StackPanel x:Name="DrawingButtonsPanel" Margin="0,8,0,0" Orientation="Horizontal" Visibility="Collapsed"> <Button Margin="0,0,6,0" Click="OnGeometryEditorCompleted" Content="Complete" /> <Button Click="OnGeometryEditorCanceled" Content="Cancel" /> </StackPanel> </StackPanel> </Border> </Grid></UserControl>