Transaction Class

Transactions are context-like objects that guard any changes made to a Revit model
Inheritance Hierarchy
SystemObject
  Autodesk.Revit.DBTransaction

Namespace: Autodesk.Revit.DB
Assembly: RevitAPI (in RevitAPI.dll) Version: 25.0.0.0 (25.0.0.0)
Syntax
public class Transaction : IDisposable

The Transaction type exposes the following members.

Constructors
 NameDescription
Public methodTransaction(Document) Instantiates a transaction object.
Public methodTransaction(Document, String) Instantiates a transaction object
Top
Properties
 NameDescription
Public propertyIsValidObject Specifies whether the .NET object represents a valid Revit entity.
Top
Methods
 NameDescription
Public methodCode exampleCommit Commits all changes made to the model during the transaction.
Public methodCode exampleCommit(FailureHandlingOptions) Commits all changes made to the model during the transaction.
Public methodDisposeReleases all resources used by the Transaction
Public methodEqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Public methodGetFailureHandlingOptions Returns the current failure handling options.
Public methodGetHashCodeServes as the default hash function.
(Inherited from Object)
Public methodGetName Returns the transaction's name.
Public methodGetStatus Returns the current status of the transaction.
Public methodGetTypeGets the Type of the current instance.
(Inherited from Object)
Public methodHasEnded Determines whether the transaction has ended already.
Public methodHasStarted Determines whether the transaction has been started yet.
Public methodCode exampleRollBack Rolls back all changes made to the model during the transaction.
Public methodCode exampleRollBack(FailureHandlingOptions) Rolls back all changes made to the model during the transaction.
Public methodCode exampleSetFailureHandlingOptions Sets options for handling failures to be used when the transaction is being committed or rolled back.
Public methodCode exampleSetName Sets the transaction's name.
Public methodCode exampleStart Starts the transaction.
Public methodCode exampleStart(String) Starts the transaction with an assigned name.
Public methodToStringReturns a string that represents the current object.
(Inherited from Object)
Top
Remarks

Any change to a document can only be made while there is an active transaction open for that document. Changes do not become part of the document until the active transaction is committed. Consequently, all changes made in a transaction can be rolled back either explicitly or implicitly by the transaction's destructor.

A document can have only one transaction open at any given time.

Transactions cannot be started when the document is in read-only mode, either permanently or temporarily. See the Document class methods IsReadOnly and IsModifiable for more details.

Transactions in linked documents are not permitted, for linked documents are not allowed to be modified.

If a transaction was started and not finished yet by the time the Transaction object is about to be disposed, the default destructor will roll it back automatically, thus all changes made to the document while this transaction was open will be discarded. It is not recommended to rely on this default behavior though. Instead, it is advised to always call either Commit or RollBack explicitly before the transaction object gets disposed. Please note that unless invoked explicitly the actual destruction of an object in managed code might not happen until the object is collected by the garbage collector.

Example
public void CreatingSketch(UIApplication uiApplication)
{
    Autodesk.Revit.DB.Document document = uiApplication.ActiveUIDocument.Document;
    Autodesk.Revit.ApplicationServices.Application application = uiApplication.Application;

    // Create a few geometry lines. These lines are transaction (not in the model),
    // therefore they do not need to be created inside a document transaction.
    XYZ Point1 = XYZ.Zero;
    XYZ Point2 = new XYZ(10, 0, 0);
    XYZ Point3 = new XYZ(10, 10, 0);
    XYZ Point4 = new XYZ(0, 10, 0);

    Line geomLine1 = Line.CreateBound(Point1, Point2);
    Line geomLine2 = Line.CreateBound(Point4, Point3);
    Line geomLine3 = Line.CreateBound(Point1, Point4);

    // This geometry plane is also transaction and does not need a transaction
    XYZ origin = XYZ.Zero;
    XYZ normal = new XYZ(0, 0, 1);
    Plane geomPlane = Plane.CreateByNormalAndOrigin(normal, origin);

    // In order to a sketch plane with model curves in it, we need
    // to start a transaction because such operations modify the model.

    // All and any transaction should be enclosed in a 'using'
    // block or guarded within a try-catch-finally blocks
    // to guarantee that a transaction does not out-live its scope.
    using (Transaction transaction = new Transaction(document))
    {
       if (transaction.Start("Create model curves") == TransactionStatus.Started)
       {
           // Create a sketch plane in current document
           SketchPlane sketch = SketchPlane.Create(document,geomPlane);

           // Create a ModelLine elements using the geometry lines and sketch plane
           ModelLine line1 = document.Create.NewModelCurve(geomLine1, sketch) as ModelLine;
           ModelLine line2 = document.Create.NewModelCurve(geomLine2, sketch) as ModelLine;
           ModelLine line3 = document.Create.NewModelCurve(geomLine3, sketch) as ModelLine;

           // Ask the end user whether the changes are to be committed or not
           TaskDialog taskDialog = new TaskDialog("Revit");
           taskDialog.MainContent = "Click either [OK] to Commit, or [Cancel] to Roll back the transaction.";
           TaskDialogCommonButtons buttons = TaskDialogCommonButtons.Ok | TaskDialogCommonButtons.Cancel;
           taskDialog.CommonButtons = buttons;

           if (TaskDialogResult.Ok == taskDialog.Show())
           {
               // For many various reasons, a transaction may not be committed
               // if the changes made during the transaction do not result a valid model.
               // If committing a transaction fails or is canceled by the end user,
               // the resulting status would be RolledBack instead of Committed.
               if (TransactionStatus.Committed != transaction.Commit())
               {
                  TaskDialog.Show("Failure", "Transaction could not be committed");
               }
           }
           else
           {
               transaction.RollBack();
           }
       }
    }
}
See Also