Hi,
I have problem with version changing. I used this chunk of code for it:
public static async Task ChangeVersion(Map map, Geodatabase geodatabase, string nameNewVersion)
{
await QueuedTask.Run(() =>
{
if (geodatabase == null)
return;
VersionManager versionManager = geodatabase.GetVersionManager();
ArcGIS.Core.Data.Version currentVersion = versionManager.GetCurrentVersion();
ArcGIS.Core.Data.Version toVersion = versionManager.GetVersion(nameNewVersion);
if (toVersion != null)
{
//Changing version
map.ChangeVersion(currentVersion, toVersion);
}
});
}
This works fine, if I use it at MapView. But If I try to use it at LayoutView, where I need to change source for layers in the map frame, I get this error: An unknown error occured while changing the database version.
I don't know if it is possible to do it that way. Is there even possible in SDK to change version only for one featureLayer as it was possible in ArcGIS Desktop? I can't find such possibility in ArcGIS Pro SDK...
I get the map for changing by this code:
Project proj = Project.Current;
Map map = null;
if (aprxName.Contains("vystup"))
{
LayoutProjectItem layoutProjItem = proj.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals(aprxName, StringComparison.CurrentCultureIgnoreCase));
if (layoutProjItem != null)
{
Layout lyt = await QueuedTask.Run(() => layoutProjItem.GetLayout());
var mapPane = ProApp.Panes.OfType<ILayoutPane>().FirstOrDefault(mPane => (mPane as Pane).ContentID == layoutProjItem.Path);
if (mapPane != null)
{
var pane = mapPane as Pane;
pane.Activate();
//Reference MapFrame
MapFrame mf = lyt.FindElement("ZTM_50_edit") as MapFrame;
//Reference map and layer
map = mf.Map;
}
}
}
Ok, I found solution for it, which works for each individual layer. The problem with my solution - it is much slower when I compare it with changing version layer by layer in ArcGIS Desktop. In my MXD/map there are 96 layers. Changing programmatically SDE version for all of them takes in ArcGIS Pro 28 seconds whereas in ArcMap only 4 seconds. I tried to refactor my code in ArcGIS Pro SDK to be more efficient but without any success. Any idea how to speed up the process in ArcGIS Pro SDK?
Here is my code:
protected override async void OnClick()
{
MapView mapView = MapView.Active;
Map map = mapView.Map;
if (mapView.DrawingPaused == false)
{
mapView.DrawingPaused = true;
}
string versionName = "\"DOMAIN\\MASTER\".0829_WorkingVersion";
IEnumerable<BasicFeatureLayer> layers = map.GetLayersAsFlattenedList().OfType<BasicFeatureLayer>();
try
{
foreach (BasicFeatureLayer basicFeatureLayer in layers)
{
await ChangeVersion(basicFeatureLayer, versionName);
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
if (mapView.DrawingPaused)
{
mapView.DrawingPaused = false;
}
}
public static async Task ChangeVersion(BasicFeatureLayer basicFeatureLayer, string versionName)
{
await QueuedTask.Run(() =>
{
try
{
object currentDataConnection = basicFeatureLayer.GetDataConnection();
CIMStandardDataConnection updatedDataConnection = UpdateDataConnection(versionName, currentDataConnection);
basicFeatureLayer.SetDataConnection(updatedDataConnection);
basicFeatureLayer.ClearDisplayCache();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
return Task.FromResult(Task.CompletedTask);
});
}
private static CIMStandardDataConnection UpdateDataConnection(string versionName, object currentDataConnection)
{
CIMStandardDataConnection updatedDataConnection = new CIMStandardDataConnection();
try
{
string workspaceConnectionString = null;
switch (currentDataConnection)
{
case CIMStandardDataConnection connection:
if (connection.WorkspaceFactory != WorkspaceFactory.SDE) return null;
workspaceConnectionString = connection.WorkspaceConnectionString;
updatedDataConnection.WorkspaceFactory = connection.WorkspaceFactory;
updatedDataConnection.Dataset = connection.Dataset;
updatedDataConnection.DatasetType = connection.DatasetType;
break;
case CIMFeatureDatasetDataConnection connection:
if (connection.WorkspaceFactory != WorkspaceFactory.SDE) return null;
workspaceConnectionString = connection.WorkspaceConnectionString;
updatedDataConnection.WorkspaceFactory = connection.WorkspaceFactory;
updatedDataConnection.Dataset = connection.Dataset;
updatedDataConnection.DatasetType = connection.DatasetType;
break;
default:
Console.WriteLine("I'm something else");
break;
}
List<string> infoFromConnString = workspaceConnectionString.Split(';').ToList();
//make change of a version in the connection string
infoFromConnString[4] = "VERSION=" + versionName;
string updatedConnectionStringForVersion = string.Join(";", infoFromConnString.ToArray());
updatedDataConnection.WorkspaceConnectionString = updatedConnectionStringForVersion;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
return updatedDataConnection;
}