This sample demonstrates the ability of feature services to restrict editing operations based on feature ownership. The owner is defined as the user who created the feature. In this example, owners can update or delete their features. Feature not owned by the current user cannot be updated or deleted.
The following credentials are valid user accounts for the sample service:
user1\user1
user2\user2
user3\user3
Download Sample Application
< UserControl x:Class = " ArcGISWPFSDK.OwnershipBasedEditing "
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/client/2009 " >
< Grid x:Name = " LayoutRoot " >
< Grid.Resources >
< esri : Editor x:Name = " MyEditor " x:Key = " MyEditor "
ContinuousMode = " True " />
</ Grid.Resources >
< Grid.RowDefinitions >
< RowDefinition Height = " * " />
< RowDefinition Height = " 5 " />
< RowDefinition Height = " 150 " />
</ Grid.RowDefinitions >
< esri : Map Name = " MyMap " UseAcceleratedDisplay = " True " WrapAround = " True " >
< esri : Map.Extent >
< esri : Envelope XMin = " -10603931.812 " YMin = " 3384001.576 " XMax = " -10595782.642 " YMax = " 3388315.896 " >
< esri : Envelope.SpatialReference >
< esri : SpatialReference WKID = " 102100 " />
</ esri : Envelope.SpatialReference >
</ esri : Envelope >
</ esri : Map.Extent >
< esri : ArcGISTiledMapServiceLayer ID = " Street Map "
Url = " http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer " />
< esri : FeatureLayer ID = " SaveTheBayMarineLayer "
DisplayName = " Save the Bay - Marine Layer "
Url = " http://sampleserver6.arcgisonline.com/arcgis/rest/services/SaveTheBay/FeatureServer/0 "
MouseLeftButtonDown = " FeatureLayer_MouseLeftButtonDown "
InitializationFailed = " FeatureLayer_InitializationFailed "
OutFields = " type,confirmed,comments,creator,submitted "
Mode = " OnDemand " />
</ esri : Map >
< Grid Name = " LoggedInGrid "
HorizontalAlignment = " Right "
VerticalAlignment = " Top "
Margin = " 0,15,15,0 "
Visibility = " Collapsed " >
< Rectangle Stroke = " Gray "
Fill = " LightGray "
Margin = " -10 " >
< Rectangle.Effect >
< DropShadowEffect />
</ Rectangle.Effect >
</ Rectangle >
< Grid >
< Grid.ColumnDefinitions >
< ColumnDefinition />
< ColumnDefinition />
</ Grid.ColumnDefinitions >
< Grid.RowDefinitions >
< RowDefinition />
< RowDefinition />
< RowDefinition />
</ Grid.RowDefinitions >
< TextBlock Grid.Row = " 0 "
Grid.ColumnSpan = " 2 "
Text = " {Binding ElementName=MyMap,Path=Layers[SaveTheBayMarineLayer].DisplayName} " />
< TextBlock Text = " Logged in as: "
Grid.Column = " 0 "
Grid.Row = " 1 "
Margin = " 2 "
VerticalAlignment = " Center " />
< TextBlock Name = " LoggedInUserTextBlock "
FontWeight = " Bold "
HorizontalAlignment = " Left "
Margin = " 2 "
Grid.Column = " 1 "
Grid.Row = " 1 "
TextAlignment = " Left " />
< Button Name = " ChangeUserButton "
Content = " Sign Out "
Width = " 140 "
Margin = " 0,5,0,5 "
HorizontalAlignment = " Center "
Grid.Row = " 2 "
Grid.ColumnSpan = " 2 "
Click = " SignOut_Click " />
</ Grid >
</ Grid >
< Border Name = " ToolBorder "
Background = " LightGray "
Visibility = " Collapsed "
BorderThickness = " 1 "
HorizontalAlignment = " Left "
VerticalAlignment = " Top "
Padding = " 5 "
BorderBrush = " Black "
Margin = " 5 " >
< Border.Effect >
< DropShadowEffect />
</ Border.Effect >
< StackPanel Orientation = " Vertical " >
< esri : TemplatePicker Map = " {Binding ElementName=MyMap, Mode=OneWay} " LayerIDs = " SaveTheBayMarineLayer " />
< StackPanel DataContext = " {StaticResource MyEditor} "
Orientation = " Vertical " >
< StackPanel Orientation = " Horizontal " >
< Button Command = " {Binding ClearSelection} "
Margin = " 2 " >
< TextBlock > Clear< LineBreak /> Selection</ TextBlock >
</ Button >
< Button Command = " {Binding DeleteSelected} "
Margin = " 2 " >
< TextBlock > Delete< LineBreak /> Selected</ TextBlock >
</ Button >
</ StackPanel >
</ StackPanel >
</ StackPanel >
</ Border >
< GridSplitter Grid.Row = " 1 " HorizontalAlignment = " Stretch " />
< esri : FeatureDataGrid Grid.Row = " 2 "
x:Name = " MyDataGrid "
Map = " {Binding ElementName=MyMap} "
SelectionMode = " Single "
GraphicsLayer = " {Binding ElementName=MyMap, Path=Layers.[SaveTheBayMarineLayer]} " />
</ Grid >
</ UserControl >
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Toolkit;
namespace ArcGISWPFSDK
{
public partial class OwnershipBasedEditing : UserControl
{
public OwnershipBasedEditing()
{
InitializeComponent();
IdentityManager.Current.ChallengeMethod = Challenge;
MyMap.Layers.LayersInitialized += (s, e) =>
{
ESRI.ArcGIS.Client.Editor myEditor = LayoutRoot.Resources["MyEditor" ] as ESRI.ArcGIS.Client.Editor;
myEditor.Map = MyMap;
myEditor.GeometryServiceUrl = "http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer" ;
};
}
private void Challenge(string url,
Action<IdentityManager.Credential, Exception> callback, IdentityManager.GenerateTokenOptions options)
{
SignInDialog.DoSignIn(url, (credential, error) =>
{
if (error == null )
{
ToolBorder.Visibility = System.Windows.Visibility.Visible;
LoggedInGrid.Visibility = System.Windows.Visibility.Visible;
LoggedInUserTextBlock.Text = credential.UserName;
}
callback(credential, error);
}
, options);
}
private void FeatureLayer_InitializationFailed(object sender, EventArgs e)
{ }
private void SignOut_Click(object sender, RoutedEventArgs e)
{
SignOut();
}
private void SignOut()
{
var l = MyMap.Layers["SaveTheBayMarineLayer" ] as FeatureLayer;
var credential = IdentityManager.Current.FindCredential(l.Url, LoggedInUserTextBlock.Text);
if (credential == null ) return ;
ToolBorder.Visibility = System.Windows.Visibility.Collapsed;
LoggedInGrid.Visibility = System.Windows.Visibility.Collapsed;
IdentityManager.Current.RemoveCredential(credential);
MyMap.Layers.Remove(l);
l = new FeatureLayer()
{
ID = "SaveTheBayMarineLayer" ,
DisplayName = "Save the Bay - Marine Layer" ,
Url = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/SaveTheBay/FeatureServer/0" ,
Mode = FeatureLayer.QueryMode.OnDemand
};
l.OutFields.Add("*" );
l.MouseLeftButtonDown += FeatureLayer_MouseLeftButtonDown;
l.InitializationFailed += FeatureLayer_InitializationFailed;
MyMap.Layers.Add(l);
}
private void FeatureLayer_MouseLeftButtonDown(object sender, GraphicMouseButtonEventArgs e)
{
if (e.Graphic != null && !e.Graphic.Selected && (sender as FeatureLayer).IsUpdateAllowed(e.Graphic))
{
Editor editor = LayoutRoot.Resources["MyEditor" ] as Editor;
if ((sender as FeatureLayer).IsUpdateAllowed(e.Graphic))
{
if (editor.EditVertices.CanExecute(null ))
editor.EditVertices.Execute(null );
}
else
if (editor.CancelActive.CanExecute(null ))
editor.CancelActive.Execute(null );
}
(sender as FeatureLayer).ClearSelection();
e.Graphic.Select();
MyDataGrid.ScrollIntoView(e.Graphic, null );
}
}
}
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Toolkit
Namespace ArcGISWPFSDK
Partial Public Class OwnershipBasedEditing
Inherits UserControl
Public Sub New ()
InitializeComponent()
IdentityManager.Current.ChallengeMethod = AddressOf Challenge
End Sub
Private Sub Challenge(ByVal url As String , ByVal callback As Action(Of IdentityManager.Credential, Exception), ByVal options As IdentityManager.GenerateTokenOptions)
SignInDialog.DoSignIn(url, Sub (credential, err)
If err Is Nothing Then
ToolBorder.Visibility = System.Windows.Visibility.Visible
LoggedInGrid.Visibility = System.Windows.Visibility.Visible
LoggedInUserTextBlock.Text = credential.UserName
End If
callback(credential, err)
End Sub , options)
End Sub
Private Sub FeatureLayer_InitializationFailed(ByVal sender As Object , ByVal e As EventArgs)
End Sub
Private Sub SignOut_Click(ByVal sender As Object , ByVal e As RoutedEventArgs)
SignOut()
End Sub
Private Sub SignOut()
Dim l As FeatureLayer
l = TryCast(MyMap.Layers("SaveTheBayMarineLayer" ), FeatureLayer)
Dim credential = IdentityManager.Current.FindCredential(l.Url, LoggedInUserTextBlock.Text)
If credential Is Nothing Then
Return
End If
ToolBorder.Visibility = System.Windows.Visibility.Collapsed
LoggedInGrid.Visibility = System.Windows.Visibility.Collapsed
IdentityManager.Current.RemoveCredential(credential)
MyMap.Layers.Remove(l)
l = New FeatureLayer() With {.ID = "SaveTheBayMarineLayer" , .DisplayName = "Save the Bay - Marine Layer" , .Url = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/SaveTheBay/FeatureServer/0" , .Mode = FeatureLayer.QueryMode.OnDemand}
l.OutFields.Add("*" )
AddHandler l.MouseLeftButtonDown, AddressOf FeatureLayer_MouseLeftButtonDown
AddHandler l.InitializationFailed, AddressOf FeatureLayer_InitializationFailed
MyMap.Layers.Add(l)
End Sub
Private Sub FeatureLayer_MouseLeftButtonDown(ByVal sender As Object , ByVal e As GraphicMouseButtonEventArgs)
If e.Graphic IsNot Nothing AndAlso (Not e.Graphic.Selected) AndAlso (TryCast(sender, FeatureLayer)).IsUpdateAllowed(e.Graphic) Then
Dim editor As Editor = TryCast(LayoutRoot.Resources("MyEditor" ), Editor)
If (TryCast(sender, FeatureLayer)).IsUpdateAllowed(e.Graphic) Then
If editor.EditVertices.CanExecute(Nothing ) Then
editor.EditVertices.Execute(Nothing )
End If
Else
If editor.CancelActive.CanExecute(Nothing ) Then
editor.CancelActive.Execute(Nothing )
End If
End If
End If
TryCast(sender, FeatureLayer).ClearSelection()
e.Graphic.Select ()
MyDataGrid.ScrollIntoView(e.Graphic, Nothing )
End Sub
End Class
End Namespace
5/16/2014