ArcGIS Pro SDK for .NET points to the old FeatureClass after changing Data Source of FeatureLayer

1717
11
03-11-2019 08:52 AM
AveVill
New Contributor II

Hi, Esri Technical Support

I am using ArcGIS Pro SDK for .NET version 2.2.0.12813.

Based on community sample ChangeLayerDataSource.cs I have implemented Data Source change functionality using SetDataConnection method.

featureLayer.SetDataConnection(updatedDataConnection);

Everything works fine except the cases when WorkspaceFactory changes from FileGDB to SDE or the other way around, from SDE to FileGDB. After setting the new Data Connection, the new parameters are attached to the featureLayer, but when reading the layers again with MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>(), the featureLayer.GetFeatureClass() shows the old FeatureClass. After closing/saving and reopening the project the FeatureClass is updated and SDK shows it correctly with featureLayer.GetFeatureClass().

Has anyone had a similar issue and is there a way to refresh FeatureClass programmatically to be used in SDK?

Thanks in advance,

Ave

11 Replies
ØysteinKristoffersen
New Contributor II

Hi Rich!

I have been testing a bit recently with 2.4.0 and the problem persists for me. Originally I was doing featureLayer.FindAndReplaceWorkspacePath, and this throws the ArcGIS.Desktop.Mapping.MappingException (also in 2.4.0). However I think a more correct way for me is to do a currentMapView.FindAndReplaceWorkspacePath. I have implemented this now and an exception is not thrown, however no datasources are switched either. Nothing happens. Also notice that switching from SDE -> SDE is working fine for me. The following code is my current switch source function:

private void ChangeSourceByMap(MapView mapView) { Map map = mapView.Map; Geodatabase gdb; GDBProjectItem newgdb; foreach (KeyValuePair<connectionitem, gdbprojectitem=""> datasource in this.uniqueSourceDict) { newgdb = datasource.Value; if (newgdb == null) continue; string workspaceString = datasource.Key.ConnectionString; string suffix = Path.GetExtension(newgdb.Path).ToLower(); if (suffix == ".sde") gdb = new Geodatabase(new DatabaseConnectionFile(new Uri(newgdb.Path))); else gdb = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(newgdb.Path))); map.FindAndReplaceWorkspacePath( workspaceString, gdb.GetConnectionString(), true); } }

Thanks for your help!

Øystein

0 Kudos
UmaHarano
Esri Regular Contributor

Hi,

FindAndReplaceWorkspacePath does not support changing workspace types - FileGDB to SDK and vice versa.  Instead, use ReplaceDataSource method. 

//This is the existing layer for which we want to switch the underlying datasource
var lyr = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
QueuedTask.Run(() => {
    using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri(@"Full path to SDConnectionFile.sde")))) {
       using (Dataset dataset = geodatabase.OpenDataset<FeatureClass>("ArcGIS.DBO.TESTPOINTS")) {
    lyr.ReplaceDataSource(dataset);
       }
    }
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Thanks

Uma

0 Kudos