Feature not created in Featurelayer (Feature service) after adding field values programmatically

867
2
Jump to solution
02-21-2021 07:08 PM
PrashantKirpan
Occasional Contributor

Hi,

I'm trying to add some field values programmatically after feature is created. Code is working fine for feature class but not working with feature layer created from FeatureService.

After creating feature I assigned field value and can see feature is getting added in feature layer attribute table in Arcpro 2.7 but doesn't sync online. I manually saved all edit's but still data is not appearing in feature service. After refresh or zoom-in/out feature gets cleated from Arcpro attribute table also.

Here is my code:

{
 var mapProjItem = Project.Current.GetItems<MapProjectItem>().FirstOrDefault(item => item.Name.Contains("Map"));
 if (mapProjItem == null)
   return;

            QueuedTask.Run(() =>
            {
                var theMap = mapProjItem.GetMap();
                
                IEnumerable<FeatureLayer> featLayer = theMap.GetLayersAsFlattenedList().OfType<FeatureLayer>();
                foreach (FeatureLayer item in featLayer)
                { 
                   var layerTable = item.GetTable();
                   _rowCreateToken = RowCreatedEvent.Subscribe(onRowCreateEvent, layerTable);                        
             }

}
private Guid _currentRowChangedGuid = Guid.Empty;
private void onRowCreateEvent(RowChangedEventArgs args)
        {
           if (_currentRowChangedGuid == args.Guid)
              return;
            
           var row = args.Row;
           row["fieldname"] = "Some Value";
           _currentRowChangedGuid = args.Guid;
           row.Store();
           _currentRowChangedGuid = Guid.Empty; 
}

 

Is there anything additional I need to do to save feature service edits?

Any help would be appreciated.

-Prashant 

0 Kudos
1 Solution

Accepted Solutions
KirkKuykendall1
Occasional Contributor III

In a previous post (now deleted) I incorrectly assumed an OnChangeRow event was needed to make an attribute change stick.  I was wrong.  I now see that EditOperation.Modify works within the OnCreateRow event.  This is similar to the way ArcObjects requires editoperations.

private void Test4()
{
    var mapProjItem = Project.Current.GetItems<MapProjectItem>()
        .FirstOrDefault(item => item.Name.Contains("Map"));
    if (mapProjItem == null)
        return; // (no "Map" found)
    QueuedTask.Run(() =>
    {
        var featTable = mapProjItem.GetMap()
            .GetLayersAsFlattenedList()
            .OfType<FeatureLayer>()
            .Select(fLayer => fLayer.GetTable())
            .FirstOrDefault(t => t.GetDataConnection() is CIMStandardDataConnection);
        if (featTable == null)
            return; // (no featureservice layer found)

        var token1 = RowCreatedEvent.Subscribe(ea =>
        {
            // this doesn't stick ...
            //ea.Row["DisplayName"] = DateTime.Now.Ticks;

            // but this does (same way arcobjects needs an editoperation)...
            ea.Operation.Modify(ea.Row, "DisplayName", DateTime.Now.Ticks);
        }, featTable);
    });
}

View solution in original post

0 Kudos
2 Replies
KirkKuykendall1
Occasional Contributor III

I wonder if what you're seeing is related to this issue:

https://community.esri.com/t5/arcgis-pro-sdk-questions/rowcreatedevent-has-different-behavior-than-r...

Perhaps "We'll work on these" means a bug tracking number was assigned to it?

0 Kudos
KirkKuykendall1
Occasional Contributor III

In a previous post (now deleted) I incorrectly assumed an OnChangeRow event was needed to make an attribute change stick.  I was wrong.  I now see that EditOperation.Modify works within the OnCreateRow event.  This is similar to the way ArcObjects requires editoperations.

private void Test4()
{
    var mapProjItem = Project.Current.GetItems<MapProjectItem>()
        .FirstOrDefault(item => item.Name.Contains("Map"));
    if (mapProjItem == null)
        return; // (no "Map" found)
    QueuedTask.Run(() =>
    {
        var featTable = mapProjItem.GetMap()
            .GetLayersAsFlattenedList()
            .OfType<FeatureLayer>()
            .Select(fLayer => fLayer.GetTable())
            .FirstOrDefault(t => t.GetDataConnection() is CIMStandardDataConnection);
        if (featTable == null)
            return; // (no featureservice layer found)

        var token1 = RowCreatedEvent.Subscribe(ea =>
        {
            // this doesn't stick ...
            //ea.Row["DisplayName"] = DateTime.Now.Ticks;

            // but this does (same way arcobjects needs an editoperation)...
            ea.Operation.Modify(ea.Row, "DisplayName", DateTime.Now.Ticks);
        }, featTable);
    });
}
0 Kudos