ArcGIS Pro 3.8 API Reference Guide
ArcGIS.Core.Data Namespace / Geodatabase Class / GetVersionManager Method
Example

In This Topic
    GetVersionManager Method
    In This Topic
    Gets the VersionManager associated with this geodatabase if it supports versioning. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public VersionManager GetVersionManager()
    Public Function GetVersionManager() As VersionManager

    Return Value

    The VersionManager associated with this geodatabase if it supports versioning.
    Exceptions
    ExceptionDescription
    This geodatabase does not support versioning.
    Remarks
    If IsVersioningSupported returns false, calling this method will result in an exception.
    Example
    Illustrate version conflict information from a reconcile operation
    {
      Task GetVersionConflictsInfoInUpdateDeleteTypeAsync(
        ServiceConnectionProperties featureServiceConnectionProperties,
        string featureClassName) => QueuedTask.Run(() =>
      {
        // To illustrate the conflict between versions,
        // the feature is updated in the child version and deleted in the parent version.
    
        long featureObjectIDForEdit = Int64.MinValue;
    
        // Get branch versioned service
        using (Geodatabase fsGeodatabase = new Geodatabase(featureServiceConnectionProperties))
        using (VersionManager versionManager = fsGeodatabase.GetVersionManager())
        using (Version defaultVersion = versionManager.GetDefaultVersion())
        using (Geodatabase defaultGeodatabase = defaultVersion.Connect())
        using (FeatureClass defaultFeatureClass = defaultGeodatabase.OpenDataset<FeatureClass>(featureClassName))
        using (FeatureClassDefinition defaultFeatureClassDefinition = defaultFeatureClass.GetDefinition())
        {
          // Create a feature in the default version to edit in a branch
          defaultGeodatabase.ApplyEdits(() =>
          {
            using (RowBuffer rowBuffer = defaultFeatureClass.CreateRowBuffer())
            {
              rowBuffer["NAME"] = "Loblolly Pine";
              rowBuffer["TREEAGE"] = 1;
              rowBuffer[defaultFeatureClassDefinition.GetShapeField()] = new MapPointBuilderEx(new Coordinate2D(1, 1),
                SpatialReferenceBuilder.CreateSpatialReference(4152, 0)).ToGeometry();
    
              using (Feature feature = defaultFeatureClass.CreateRow(rowBuffer))
              {
                featureObjectIDForEdit = feature.GetObjectID();
              }
            }
          });
          // Add newly created feature in the filter
          QueryFilter queryFilter = new QueryFilter { ObjectIDs = new List<long> { featureObjectIDForEdit } };
    
          // Create a branch version
          VersionDescription versionDescription = new VersionDescription("UpdateDeleteConflictType",
            "Update-Delete version conflict type", VersionAccessType.Public);
    
          // Edit the feature in the branch
          using (Version editVersion = versionManager.CreateVersion(versionDescription))
          using (Geodatabase branchGeodatabase = editVersion.Connect())
          using (FeatureClass featureClass = branchGeodatabase.OpenDataset<FeatureClass>(featureClassName))
          using (RowCursor rowCursor = featureClass.Search(queryFilter, false))
          {
            branchGeodatabase.ApplyEdits(() =>
            {
              while (rowCursor.MoveNext())
              {
                using (Row row = rowCursor.Current)
                {
                  row["TREEAGE"] = 100;
                  row["NAME"] = $"{row["Name"]}_EditInBranch";
                  row.Store();
                }
              }
            });
    
            // Delete the feature from the default version
            defaultGeodatabase.ApplyEdits(() =>
            {
              defaultFeatureClass.DeleteRows(queryFilter);
            });
    
            // Reconcile options
            ReconcileOptions reconcileOptions = new ReconcileOptions(defaultVersion)
            {
              ConflictResolutionType = ConflictResolutionType.FavorEditVersion,
              ConflictDetectionType = ConflictDetectionType.ByRow,
              ConflictResolutionMethod = ConflictResolutionMethod.Continue
            };
    
            // Reconcile with default
            ReconcileResult reconcileResult = editVersion.Reconcile(reconcileOptions);
    
            // Check for conflicts
            bool hasConflictsReconcileResults = reconcileResult.HasConflicts;
            bool hasConflictsAfterReconcile = editVersion.HasConflicts();
    
            // Fetch conflicts
            IReadOnlyList<Conflict> conflictsAfterReconcile = editVersion.GetConflicts();
    
            // Iterate conflicts
            foreach (Conflict conflict in conflictsAfterReconcile)
            {
              // Object ID of row where conflict occurs
              long objectId = conflict.ObjectID;
    
              ConflictType conflictType = conflict.ConflictType;
    
              IReadOnlyList<FieldValue> ancestorVersionValues = conflict.AncestorVersionValues;
              object nameAncestor = ancestorVersionValues.FirstOrDefault(f => f.FieldName.Contains("NAME")).Value;
              object treeAgeAncestor =
                ancestorVersionValues.FirstOrDefault(f => f.FieldName.Contains("TREEAGE")).Value;
    
              IReadOnlyList<FieldValue> childVersionValues = conflict.ChildVersionValues;
              object nameChild = childVersionValues.FirstOrDefault(f => f.FieldName.Contains("NAME")).Value;
              object treeAgeChild = childVersionValues.FirstOrDefault(f => f.FieldName.Contains("TREEAGE")).Value;
    
              IReadOnlyList<FieldValue> parentVersionValues = conflict.ParentVersionValues;
    
              IReadOnlyList<Field> originalFields = defaultFeatureClassDefinition.GetFields();
    
              string datasetName = conflict.DatasetName;
            }
          }
        }
      });
    }
    Connecting to a Version
    {
      Task<Geodatabase> ConnectToVersionAsync(Geodatabase geodatabase, string versionName) => QueuedTask.Run(() =>
      {
        Geodatabase connectedVersion = null;
    
        if (geodatabase.IsVersioningSupported())
        {
          using (VersionManager versionManager = geodatabase.GetVersionManager())
          using (Version version = versionManager.GetVersion(versionName))
          {
            connectedVersion = version.Connect();
          }
        }
    
        return connectedVersion;
      });
    }
    Reconciling and Posting a Version with its Parent in separate edit sessions
    {
      Task ReconcileAndPostAsync(Geodatabase geodatabase) => QueuedTask.Run(() =>
      {
        // Get a reference to our version and our parent
        if (geodatabase.IsVersioningSupported())
        {
          using (VersionManager versionManager = geodatabase.GetVersionManager())
          using (Version currentVersion = versionManager.GetCurrentVersion())
          using (Version parentVersion = currentVersion.GetParent())
          {
            // Create a ReconcileOptions object
            ReconcileOptions reconcileOptions = new ReconcileOptions(parentVersion);
            reconcileOptions.ConflictResolutionMethod = ConflictResolutionMethod.Continue; // continue if conflicts are found
            reconcileOptions.ConflictDetectionType = ConflictDetectionType.ByRow; //Default
            reconcileOptions.ConflictResolutionType = ConflictResolutionType.FavorTargetVersion; //or FavorEditVersion
    
            // Reconcile
            ReconcileResult reconcileResult = currentVersion.Reconcile(reconcileOptions);
            if (!reconcileResult.HasConflicts)
            {
              //No conflicts, perform the post
              PostOptions postOptions = new PostOptions(parentVersion);
              //var postOptions = new PostOptions(); for default version
              postOptions.ServiceSynchronizationType = ServiceSynchronizationType.Synchronous; //Default
    
              // Post - Deprecated since Pro SDK 3.6 / Enterprise 12.0 . Use Reconcile(reconcileOptions, postOptions) instead.
              // currentVersion.Post(postOptions);
    
              currentVersion.Reconcile(reconcileOptions, postOptions);
            }
          }
        }
      });
    }
    Reconciling and Posting a Version with its Parent in the same edit session
    {
      Task ReconcileAndPost2Async(Geodatabase geodatabase) => QueuedTask.Run(() =>
      {
        // Get a reference to our version and our parent
        if (geodatabase.IsVersioningSupported())
        {
          using (VersionManager versionManager = geodatabase.GetVersionManager())
          using (Version currentVersion = versionManager.GetCurrentVersion())
          using (Version parentVersion = currentVersion.GetParent())
          {
            // Create a ReconcileOptions object
            ReconcileOptions reconcileOptions = new ReconcileOptions(parentVersion);
            reconcileOptions.ConflictResolutionMethod = ConflictResolutionMethod.Continue; // continue if conflicts are found
            reconcileOptions.ConflictDetectionType = ConflictDetectionType.ByRow; //Default
            reconcileOptions.ConflictResolutionType = ConflictResolutionType.FavorTargetVersion; //or FavorEditVersion
    
            PostOptions postOptions = new PostOptions(parentVersion);
            //var postOptions = new PostOptions(); for default version
            postOptions.ServiceSynchronizationType = ServiceSynchronizationType.Synchronous; //Default
    
            // Reconcile
            ReconcileResult reconcileResult = currentVersion.Reconcile(reconcileOptions, postOptions);
            if (reconcileResult.HasConflicts)
            {
              //TODO resolve conflicts
            }
          }
        }
      });
    }
    Working with Versions
    {
      await QueuedTask.Run(() =>
      {
        using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file"))))
        using (VersionManager versionManager = geodatabase.GetVersionManager())
        {
          IReadOnlyList<string> versionNames = versionManager.GetVersionNames();
    
          Version defaultVersion = versionManager.GetDefaultVersion();
    
          string testVersionName = versionNames.First(v => v.Contains("Test"));
          Version testVersion = versionManager.GetVersion(testVersionName);
    
          Version qaVersion = defaultVersion.GetChildren().First(version => version.GetName().Contains("QA"));
    
          Geodatabase qaVersionGeodatabase = qaVersion.Connect();
    
          FeatureClass currentFeatureClass = geodatabase.OpenDataset<FeatureClass>("featureClassName");
          FeatureClass qaFeatureClass = qaVersionGeodatabase.OpenDataset<FeatureClass>("featureClassName");
        }
      });
    }
    Working with the Default Version
    {
      Task<Version> FindDefaultVersionAsync(Geodatabase geodatabase) => QueuedTask.Run(() =>
      {
        if (!geodatabase.IsVersioningSupported())
        {
          return null;
        }
    
        using (VersionManager versionManager = geodatabase.GetVersionManager())
        {
          Version currentVersion = versionManager.GetCurrentVersion();
          Version defaultVersion = GetDefaultVersion(currentVersion);
          if (currentVersion != defaultVersion)
          {
            currentVersion.Dispose(); // If we are not pointing to default, we want to dispose this Version object
          }
          return defaultVersion;
        }
    
        // Check to see if the current version is default.
        // Works with both branch and traditional versioning.
        bool IsDefaultVersion(Version version)
        {
          Version parentVersion = version.GetParent();
          if (parentVersion == null)
          {
            return true;
          }
    
          parentVersion.Dispose();
          return false;
        }
    
        // Gets the default version.
        // Works with both branch and traditional versioning.
        // Note that this routine depends on IsDefaultVersion(), above.
        Version GetDefaultVersion(Version version)
        {
          if (IsDefaultVersion(version))
          {
            return version;
          }
    
          Version parent = version.GetParent();
          Version ancestor = GetDefaultVersion(parent);
          if (parent != ancestor)
          {
            parent.Dispose(); //If the versioning tree is more than 2 deep, we want to dispose any intermediary versions
          }
    
          return ancestor;
        }
      });
    }
    Creating a Version
    {
      Task<Version> CreateVersionAsync(Geodatabase geodatabase, string versionName, string description, VersionAccessType versionAccessType) => QueuedTask.Run(() =>
      {
        if (!geodatabase.IsVersioningSupported())
        {
          return null;
        }
    
        using (VersionManager versionManager = geodatabase.GetVersionManager())
        {
          VersionDescription versionDescription =
            new VersionDescription(versionName, description, versionAccessType);
          return versionManager.CreateVersion(versionDescription);
        }
      });
    }
    Creating a Historical version
    {
      Task<HistoricalVersion> CreateHistoricalVersionAsync(Geodatabase geodatabase, string versionName) => QueuedTask.Run(() =>
      {
        using (VersionManager versionManager = geodatabase.GetVersionManager())
        {
          HistoricalVersionDescription historicalVersionDescription =
            new HistoricalVersionDescription(versionName, DateTime.Now);
          HistoricalVersion historicalVersion =
            versionManager.CreateHistoricalVersion(historicalVersionDescription);
    
          return historicalVersion;
        }
      });
    }
    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);
          }
        });
      }
    }
    Requirements

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

    ArcGIS Pro version: 3.0 or higher.
    See Also