ArcGIS Pro 3.8 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / Map Class / ChangeVersion(VersionBase,VersionBase) Method
VersionBase object you want to find and replace with toVersionBase.
VersionBase object you want to replace to.
Example

In This Topic
    ChangeVersion(VersionBase,VersionBase) Method
    In This Topic
    Change GeoDatabase version of layers and standalone tables of fromVersionBase. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public void ChangeVersion( 
       VersionBase fromVersionBase,
       VersionBase toVersionBase
    )
    Public Sub ChangeVersion( _
       ByVal fromVersionBase As VersionBase, _
       ByVal toVersionBase As VersionBase _
    ) 

    Parameters

    fromVersionBase
    VersionBase object you want to find and replace with toVersionBase.
    toVersionBase
    VersionBase object you want to replace to.
    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    This operation is not supported on a map with pending edits.
    Example
    Switching between versions
    {
      Task ChangeVersionsAsync(Geodatabase geodatabase, string toVersionName)
      {
        var map = MapView.Active?.Map;
        if (map == null)
          return Task.CompletedTask;
    
        return QueuedTask.Run(() =>
        {
          using (VersionManager versionManager = geodatabase.GetVersionManager())
          {
            VersionBaseType versionBaseType = versionManager.GetCurrentVersionBaseType();
    
            if (versionBaseType == VersionBaseType.Version)
            {
              Version fromVersion = versionManager.GetCurrentVersion();
              Version toVersion = versionManager.GetVersion(toVersionName);
    
              // Switch between versions
              map.ChangeVersion(fromVersion, toVersion);
            }
    
            if (versionBaseType == VersionBaseType.HistoricalVersion)
            {
              HistoricalVersion fromHistoricalVersion = versionManager.GetCurrentHistoricalVersion();
              HistoricalVersion toHistoricalVersion = versionManager.GetHistoricalVersion(toVersionName);
    
              // Switch between historical versions
              map.ChangeVersion(fromHistoricalVersion, toHistoricalVersion);
            }
    
            // Switch from HistoricalVersion to Version and vice-versa
            // map.ChangeVersion(fromHistoricalVersion, toVersion);
            // map.ChangeVersion(fromVersion, toHistoricalVersion);
          }
        });
      }
    }
    Change Geodatabase Version of layers off a specified version in a map
    {
      await QueuedTask.Run(() =>
      {
        //Getting the current version name from the first feature layer of the map
        Datastore dataStore = featureLayer.GetFeatureClass().GetDatastore();  //getting datasource
        Geodatabase geodatabase = dataStore as Geodatabase; //casting to Geodatabase
        if (geodatabase == null)
        {
          return;
        }
    
      VersionManager versionManager = geodatabase.GetVersionManager();
      ArcGIS.Core.Data.Version currentVersion = versionManager.GetCurrentVersion();
      string currentVersionName = currentVersion.GetName();
    
      //Getting all available versions except the current one
      List<ArcGIS.Core.Data.Version> versions = [];
      foreach (string versionName in versionManager.GetVersionNames())
      {
        if (versionName != currentVersionName)
        {
          break;
        }
    
        versions.Add(versionManager.GetVersion(versionName));
      }
    
      //Assuming there is at least one other version we pick the first one from the list
      ArcGIS.Core.Data.Version toVersion = versions.FirstOrDefault();
        if (toVersion != null)
        {
          //Changing version
          map.ChangeVersion(currentVersion, toVersion);
        }
      });
    }
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also