How to get the database version from a feature layer

271
1
Jump to solution
04-27-2023 12:14 PM
Amadeus111
Occasional Contributor II

I am trying to get the version of the geodatabase of a feature layer. I couldnt figure out

so far I have this 

 var featureLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
                CIMDataConnection currentDataConnection = featureLayer.GetDataConnection();
                string connString = null;
                var fufu = currentDataConnection.ReadXml;
                var dd = fufu.Target;

I am trying to get the conn string and create Geodatabse object but no luck. Anybody knows how to get the version?

0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Probably a bit overkill but you can find what you need after line 120:

namespace LayerGeodatabaseVersion
{  
  /// <summary>
  /// Extension methods for feature layer
  /// </summary>
  internal static class FeatureLayerExtensions
  {
    public static GeodatabaseType? GetGeodatabaseType(this BasicFeatureLayer layer)
    {
      if (layer.ConnectionStatus == ConnectionStatus.Broken)
        return null;
      GeodatabaseType? gdbType = GeodatabaseType.FileSystem;
      using (var dataset = layer.GetTable())
      {
        using (var gdb = dataset.GetDatastore())
        {
          //new at 2.3
          if (gdb is PluginDatastore)
          {
            return null;
          }
          //Note shapefile will be "FileSystemDatastore"
          if (gdb is Geodatabase)
          {
            gdbType = ((Geodatabase)gdb).GetGeodatabaseType();
          }
        }
      }
      return gdbType;
    }

    public static RegistrationType? GetRegistrationType(this BasicFeatureLayer layer)
    {
      if (layer.ConnectionStatus == ConnectionStatus.Broken)
        return null;

      RegistrationType? regType = null;
      using (var dataset = layer.GetTable())
      {
        regType = dataset.GetRegistrationType();
      }
      return regType;
    }

    public static EditOperationType? GetEditOperationType(this BasicFeatureLayer layer)
    {
      if (layer.ConnectionStatus == ConnectionStatus.Broken)
        return null;
      var gdbType = layer.GetGeodatabaseType();
      var regType = layer.GetRegistrationType();

      //Plugin - new at 2.3
      if (gdbType == null)
        return null;
      //FileSystem
      if (gdbType == GeodatabaseType.FileSystem)
        return EditOperationType.Long;
      //LocalDatabase
      if (gdbType == GeodatabaseType.LocalDatabase)
        return EditOperationType.Long;
      //RemoteDatabase, Versioned
      if (gdbType == GeodatabaseType.RemoteDatabase && (
        regType == RegistrationType.Versioned || regType == RegistrationType.VersionedWithMoveToBase))
        return EditOperationType.Long;
      //RemoteDatabase, NonVersioned
      if (gdbType == GeodatabaseType.RemoteDatabase && regType == RegistrationType.Nonversioned)
        return EditOperationType.Short;
      //Service, NonVersioned
      if (gdbType == GeodatabaseType.Service && regType == RegistrationType.Nonversioned)
        return EditOperationType.Short;

      //Service, Versioned, Default
      EditOperationType forBranch = EditOperationType.Long;
      if (gdbType == GeodatabaseType.Service && regType == RegistrationType.Versioned)
      {
        using (var dataset = layer.GetTable())
        using (var gdb = dataset.GetDatastore() as Geodatabase)
        using (var vmgr = gdb.GetVersionManager())
        using (var vers = vmgr.GetCurrentVersion())
        using (var parent = vers.GetParent())
        {
          //non-default supports undo/redo and save/discard. Default does not
          forBranch = parent != null ? EditOperationType.Long : EditOperationType.Short;
        }
      }
      return forBranch;
    }
  }

  internal class ShowLayerGDBVersion : Button
  {
    protected override async void OnClick()
    {
      try
      {
        await QueuedTask.Run(() =>
        {
          FeatureLayer theLayer = null;
          var selectedLayers = MapView.Active.GetSelectedLayers();
          if (selectedLayers.Count > 0)
          {
            theLayer = selectedLayers[0] as FeatureLayer;
          }
          if (theLayer == null)
          {
            theLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
          }
          if (theLayer == null)
          {
            throw new Exception("A Feature Layer is required.");
          }
          var gdbType = theLayer.GetGeodatabaseType();
          var regType = theLayer.GetRegistrationType();

          switch (gdbType.Value)
          {
            case GeodatabaseType.LocalDatabase:
              MessageBox.Show($@"Feature Layer {theLayer.Name} is a local GDB datasource");
              break;
            case GeodatabaseType.RemoteDatabase:
              using (Geodatabase geodatabase = theLayer.GetTable().GetDatastore() as Geodatabase)
              using (Table table = theLayer.GetTable())
              {
                if (geodatabase == null)
                {
                  throw new Exception($@"Feature Layer {theLayer.Name} is not stored in a Geodatabase");
                }
                var dbConnectionProps = (DatabaseConnectionProperties)geodatabase.GetConnector();
                var nl = System.Environment.NewLine;
                var dbConnectionDisp = $@"{theLayer.Name}:{nl} Database:{dbConnectionProps.Database}";
                dbConnectionDisp += $@" Instance: {dbConnectionProps.Instance}";
                dbConnectionDisp += $@" DBMS: {dbConnectionProps.DBMS}";
                dbConnectionDisp += $@" RegType: {regType}";
                dbConnectionDisp += $@" Version: {dbConnectionProps.Version}";
                MessageBox.Show(dbConnectionDisp);
              }
              break;
            case GeodatabaseType.FileSystem:
              MessageBox.Show($@"Feature Layer {theLayer.Name} is a file system datasource");
              break;
            case GeodatabaseType.Service:
              MessageBox.Show($@"Feature Layer {theLayer.Name} is a service based datasource");
              break;
            case GeodatabaseType.Memory:
              MessageBox.Show($@"Feature Layer {theLayer.Name} is a memory datasource");
              break;
            default:
              MessageBox.Show($@"Feature Layer {theLayer.Name} is a plugin datasource");
              break;
          }
        });
      }
      catch (Exception ex)
      {
        MessageBox.Show (ex.Message);
      }
    }
  }
}

 

View solution in original post

1 Reply
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Probably a bit overkill but you can find what you need after line 120:

namespace LayerGeodatabaseVersion
{  
  /// <summary>
  /// Extension methods for feature layer
  /// </summary>
  internal static class FeatureLayerExtensions
  {
    public static GeodatabaseType? GetGeodatabaseType(this BasicFeatureLayer layer)
    {
      if (layer.ConnectionStatus == ConnectionStatus.Broken)
        return null;
      GeodatabaseType? gdbType = GeodatabaseType.FileSystem;
      using (var dataset = layer.GetTable())
      {
        using (var gdb = dataset.GetDatastore())
        {
          //new at 2.3
          if (gdb is PluginDatastore)
          {
            return null;
          }
          //Note shapefile will be "FileSystemDatastore"
          if (gdb is Geodatabase)
          {
            gdbType = ((Geodatabase)gdb).GetGeodatabaseType();
          }
        }
      }
      return gdbType;
    }

    public static RegistrationType? GetRegistrationType(this BasicFeatureLayer layer)
    {
      if (layer.ConnectionStatus == ConnectionStatus.Broken)
        return null;

      RegistrationType? regType = null;
      using (var dataset = layer.GetTable())
      {
        regType = dataset.GetRegistrationType();
      }
      return regType;
    }

    public static EditOperationType? GetEditOperationType(this BasicFeatureLayer layer)
    {
      if (layer.ConnectionStatus == ConnectionStatus.Broken)
        return null;
      var gdbType = layer.GetGeodatabaseType();
      var regType = layer.GetRegistrationType();

      //Plugin - new at 2.3
      if (gdbType == null)
        return null;
      //FileSystem
      if (gdbType == GeodatabaseType.FileSystem)
        return EditOperationType.Long;
      //LocalDatabase
      if (gdbType == GeodatabaseType.LocalDatabase)
        return EditOperationType.Long;
      //RemoteDatabase, Versioned
      if (gdbType == GeodatabaseType.RemoteDatabase && (
        regType == RegistrationType.Versioned || regType == RegistrationType.VersionedWithMoveToBase))
        return EditOperationType.Long;
      //RemoteDatabase, NonVersioned
      if (gdbType == GeodatabaseType.RemoteDatabase && regType == RegistrationType.Nonversioned)
        return EditOperationType.Short;
      //Service, NonVersioned
      if (gdbType == GeodatabaseType.Service && regType == RegistrationType.Nonversioned)
        return EditOperationType.Short;

      //Service, Versioned, Default
      EditOperationType forBranch = EditOperationType.Long;
      if (gdbType == GeodatabaseType.Service && regType == RegistrationType.Versioned)
      {
        using (var dataset = layer.GetTable())
        using (var gdb = dataset.GetDatastore() as Geodatabase)
        using (var vmgr = gdb.GetVersionManager())
        using (var vers = vmgr.GetCurrentVersion())
        using (var parent = vers.GetParent())
        {
          //non-default supports undo/redo and save/discard. Default does not
          forBranch = parent != null ? EditOperationType.Long : EditOperationType.Short;
        }
      }
      return forBranch;
    }
  }

  internal class ShowLayerGDBVersion : Button
  {
    protected override async void OnClick()
    {
      try
      {
        await QueuedTask.Run(() =>
        {
          FeatureLayer theLayer = null;
          var selectedLayers = MapView.Active.GetSelectedLayers();
          if (selectedLayers.Count > 0)
          {
            theLayer = selectedLayers[0] as FeatureLayer;
          }
          if (theLayer == null)
          {
            theLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
          }
          if (theLayer == null)
          {
            throw new Exception("A Feature Layer is required.");
          }
          var gdbType = theLayer.GetGeodatabaseType();
          var regType = theLayer.GetRegistrationType();

          switch (gdbType.Value)
          {
            case GeodatabaseType.LocalDatabase:
              MessageBox.Show($@"Feature Layer {theLayer.Name} is a local GDB datasource");
              break;
            case GeodatabaseType.RemoteDatabase:
              using (Geodatabase geodatabase = theLayer.GetTable().GetDatastore() as Geodatabase)
              using (Table table = theLayer.GetTable())
              {
                if (geodatabase == null)
                {
                  throw new Exception($@"Feature Layer {theLayer.Name} is not stored in a Geodatabase");
                }
                var dbConnectionProps = (DatabaseConnectionProperties)geodatabase.GetConnector();
                var nl = System.Environment.NewLine;
                var dbConnectionDisp = $@"{theLayer.Name}:{nl} Database:{dbConnectionProps.Database}";
                dbConnectionDisp += $@" Instance: {dbConnectionProps.Instance}";
                dbConnectionDisp += $@" DBMS: {dbConnectionProps.DBMS}";
                dbConnectionDisp += $@" RegType: {regType}";
                dbConnectionDisp += $@" Version: {dbConnectionProps.Version}";
                MessageBox.Show(dbConnectionDisp);
              }
              break;
            case GeodatabaseType.FileSystem:
              MessageBox.Show($@"Feature Layer {theLayer.Name} is a file system datasource");
              break;
            case GeodatabaseType.Service:
              MessageBox.Show($@"Feature Layer {theLayer.Name} is a service based datasource");
              break;
            case GeodatabaseType.Memory:
              MessageBox.Show($@"Feature Layer {theLayer.Name} is a memory datasource");
              break;
            default:
              MessageBox.Show($@"Feature Layer {theLayer.Name} is a plugin datasource");
              break;
          }
        });
      }
      catch (Exception ex)
      {
        MessageBox.Show (ex.Message);
      }
    }
  }
}