ArcGIS Pro 3.7 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 Sub ChangeVersion( _
       ByVal fromVersionBase As VersionBase, _
       ByVal toVersionBase As VersionBase _
    ) 
    public void ChangeVersion( 
       VersionBase fromVersionBase,
       VersionBase toVersionBase
    )

    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
    {
      // Must be called within QueuedTask.Run
      void ChangeVersions(Geodatabase geodatabase, string toVersionName)
      {
        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
            MapView.Active.Map.ChangeVersion(fromVersion, toVersion);
          }
    
          if (versionBaseType == VersionBaseType.HistoricalVersion)
          {
            HistoricalVersion fromHistoricalVersion = versionManager.GetCurrentHistoricalVersion();
            HistoricalVersion toHistoricalVersion = versionManager.GetHistoricalVersion(toVersionName);
    
            // Switch between historical versions
            MapView.Active.Map.ChangeVersion(fromHistoricalVersion, toHistoricalVersion);
          }
    
          // Switch from HistoricalVersion to Version and vice-versa 
          // MapView.Active.Map.ChangeVersion(fromHistoricalVersion, toVersion);
          // MapView.Active.Map.ChangeVersion(fromVersion, toHistoricalVersion);
        }
      }
    }
    Change Geodatabase Version of layers off a specified version in a map
    {
        //Note: needs to be called on the MCT
        //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
            MapView.Active.Map.ChangeVersion(currentVersion, toVersion);
        }
    }
    Requirements

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

    ArcGIS Pro version: 3.0 or higher.
    See Also