ArcGIS Pro 3.4 API Reference Guide
ArcGIS.Desktop.Editing Namespace / EditOperation Class / CreateChainedOperation Method
Example

In This Topic
    CreateChainedOperation Method
    In This Topic
    Creates a follow-on operation which will be shared with this operation on the application's undo stack.
    Syntax
    public EditOperation CreateChainedOperation()
    Public Function CreateChainedOperation() As EditOperation

    Return Value

    A newly created EditOperation that will be merged with this one.
    Remarks
    Call CreateChainedOperation once the parent edit operation is known to have executed successfully.
    Example
    Edit Operation Duplicate Features
    {
      var duplicateFeatures = new EditOperation() { Name = "Duplicate Features" };
    
      //Duplicate with an X and Y offset of 500 map units
      //At 2.x duplicateFeatures.Duplicate(featureLayer, oid, 500.0, 500.0, 0.0);
    
      //Execute to execute the operation
      //Must be called within QueuedTask.Run
      var insp2 = new Inspector();
      insp2.Load(featureLayer, oid);
      var geom = insp2["SHAPE"] as Geometry;
    
      var rtoken = duplicateFeatures.Create(insp2.MapMember, insp2.ToDictionary(a => a.FieldName, a => a.CurrentValue));
      if (!duplicateFeatures.IsEmpty)
      {
        if (duplicateFeatures.Execute())//Execute and ExecuteAsync will return true if the operation was successful and false if not
        {
          var modifyOp = duplicateFeatures.CreateChainedOperation();
          modifyOp.Modify(featureLayer, (long)rtoken.ObjectID, GeometryEngine.Instance.Move(geom, 500.0, 500.0));
          if (!modifyOp.IsEmpty)
          {
            var result = modifyOp.Execute();
          }
        }
      }
    }
    
    Edit Operation Chain Edit Operations
    //Chaining operations is a special case. Use "Chained Operations" when you require multiple transactions 
    //to be undo-able with a single "Undo".
    
    //The most common use case for operation chaining is creating a feature with an attachment. 
    //Adding an attachment requires the object id (of a new feature) has already been created. 
    var editOperation1 = new EditOperation() { Name = string.Format("Create point in '{0}'", CurrentTemplate.Layer.Name) };
    
    long newFeatureID = -1;
    //The Create operation has to execute so we can get an object_id
    var token2 = editOperation1.Create(this.CurrentTemplate, polygon);
    
    //Must be within a QueuedTask
    editOperation1.Execute(); //Note: Execute and ExecuteAsync will return true if the operation was successful and false if not
    if (editOperation1.IsSucceeded)
    {
      newFeatureID = (long)token2.ObjectID;
      //Now, because we have the object id, we can add the attachment.  As we are chaining it, adding the attachment 
      //can be undone as part of the "Undo Create" operation. In other words, only one undo operation will show on the 
      //Pro UI and not two.
      var editOperation2 = editOperation1.CreateChainedOperation();
      //Add the attachment using the new feature id
      editOperation2.AddAttachment(this.CurrentTemplate.Layer, newFeatureID, @"C:\data\images\Hydrant.jpg");
      //Execute the chained edit operation. editOperation1 and editOperation2 show up as a single Undo operation
      //on the UI even though we had two transactions
      editOperation2.Execute();
    }
    
    Create a new Relationship and New Entities 1
    var create_rel1 = await QueuedTask.Run(() =>
    {
      //This example uses a chained edit operation
      var edit_op = new EditOperation()
      {
        Name = "Create entities and a relationship",
        SelectNewFeatures = true
      };
    
      //We are just going to use the GDB objects in this one but
      //we could equally use feature layers/standalone tables
    
      //using Feature Class/Tables (works within Investigation or map)
      var org_fc = kg.OpenDataset<FeatureClass>("Organization");
      var person_tbl = kg.OpenDataset<Table>("Person");
      //Relationship table
      var emp_tbl = kg.OpenDataset<Table>("HasEmployee");
    
      var attribs = new Dictionary<string, object>();
    
      //New Organization
      attribs["Name"] = "Acme Ltd.";
      attribs["Description"] = "Specializes in household items";
      attribs["SHAPE"] = org_location;
    
      //Add it to the operation - we need the rowtoken
      var rowtoken = edit_op.Create(org_fc, attribs);
    
      attribs.Clear();//we are going to re-use the dictionary
    
      //New Person
      attribs["Name"] = "Bob";
      attribs["Age"] = "41";
      attribs["Skills"] = "Plumbing, Framing, Flooring";
    
      //Add it to the operation
      var rowtoken2 = edit_op.Create(person_tbl, attribs);
    
      attribs.Clear();
    
      //At this point we must execute the create of the entities
      if (edit_op.Execute())
      {
        //if we are here, the create of the entities was successful
    
        //Next, "chain" a second create for the relationship - this ensures that
        //Both creates (entities _and_ relation) will be -undone- together if needed
        //....in other words they will behave as if they are a -single- transaction
        var edit_op_rel = edit_op.CreateChainedOperation();
    
        //we need the names of the origin and destination relation properties
        var kg_prop_info = kg.GetPropertyNameInfo();
        //use the row tokens we held on to from the entity creates
        attribs[kg_prop_info.OriginIDPropertyName] = rowtoken.GlobalID;
        attribs[kg_prop_info.DestinationIDPropertyName] = rowtoken2.GlobalID;
    
        //Add any extra attribute information for the relation as needed
        attribs["StartDate"] = new DateTimeOffset(DateTime.Now);
    
        //Do the create of the relate
        edit_op_rel.Create(emp_tbl, attribs);
        return edit_op_rel.Execute();
      }
      return false;//Create of entities failed
    });
    
    Create a Document Record
    internal static string GetDocumentTypeName(KnowledgeGraphDataModel kg_dm)
    {
      var entity_types = kg_dm.GetEntityTypes();
      foreach (var entity_type in entity_types)
      {
        var role = entity_type.Value.GetRole();
        if (role == KnowledgeGraphNamedObjectTypeRole.Document)
          return entity_type.Value.GetName();
      }
      return "";
    }
    
    internal static string GetHasDocumentTypeName(KnowledgeGraphDataModel kg_dm)
    {
      var rel_types = kg_dm.GetRelationshipTypes();
      foreach (var rel_type in rel_types)
      {
        var role = rel_type.Value.GetRole();
        if (role == KnowledgeGraphNamedObjectTypeRole.Document)
          return rel_type.Value.GetName();
      }
      return "";
    }
    
    internal async void AddDocumentRecord()
    {
    
      await QueuedTask.Run(() =>
      {
        using (var kg = GetKnowledgeGraph())
        {
          var edit_op = new EditOperation()
          {
            Name = "Create Document Example",
            SelectNewFeatures = true
          };
    
          var doc_entity_name = GetDocumentTypeName(kg.GetDataModel());
          if (string.IsNullOrEmpty(doc_entity_name))
            return false;
          var hasdoc_rel_name = GetHasDocumentTypeName(kg.GetDataModel());
          if (string.IsNullOrEmpty(hasdoc_rel_name))
            return false;
    
          //Document can also be FeatureClass
          var doc_tbl = kg.OpenDataset<Table>(doc_entity_name);
          var doc_rel_tbl = kg.OpenDataset<Table>(hasdoc_rel_name);
    
          //This is the document to be added...file, image, resource, etc.
          var url = @"E:\Data\Temp\HelloWorld.txt";
          var text = System.IO.File.ReadAllText(url);
    
          //Set document properties
          var attribs = new Dictionary<string, object>();
          attribs["contentType"] = @"text/plain";
          attribs["name"] = System.IO.Path.GetFileName(url);
          attribs["url"] = url;
          //Add geometry if relevant
          //attribs["Shape"] = doc_location;
    
          //optional
          attribs["fileExtension"] = System.IO.Path.GetExtension(url);
          attribs["text"] = System.IO.File.ReadAllText(url);
    
          //optional and arbitrary - your choice
          attribs["title"] = System.IO.Path.GetFileNameWithoutExtension(url);
          attribs["keywords"] = @"text,file,example";
          attribs["metadata"] = "";
    
          //Specify any additional custom attributes added to the document
          //schema by the user as needed....
          //attribs["custom_attrib"] = "Foo";
          //attribs["custom_attrib2"] = "Bar";
    
          //Get the entity whose document this is...
          var org_fc = kg.OpenDataset<FeatureClass>("Organization");
          var qf = new QueryFilter()
          {
            WhereClause = "name = 'Acme'",
            SubFields = "*"
          };
          var origin_org_id = Guid.Empty;
          using (var rc = org_fc.Search(qf))
          {
            if (!rc.MoveNext())
              return false;
            origin_org_id = rc.Current.GetGlobalID();//For the relate
          }
    
          //Create the document row/feature
          var rowtoken = edit_op.Create(doc_tbl, attribs);
          if (edit_op.Execute())
          {
            //Create the relationship row
            attribs.Clear();
            //we need the names of the origin and destination relation properties
            var kg_prop_info = kg.GetPropertyNameInfo();
            //Specify the origin entity (i.e. the document 'owner') and
            //the document being related to (i.e. the document 'itself')
            attribs[kg_prop_info.OriginIDPropertyName] = origin_org_id;//entity
            attribs[kg_prop_info.DestinationIDPropertyName] = rowtoken.GlobalID;//document
    
            //Specify any custom attributes added to the has document
            //schema by the user as needed....
            //attribs["custom_attrib"] = "Foo";
            //attribs["custom_attrib2"] = "Bar";
    
            //"Chain" a second create for the relationship - this ensures that
            //Both creates (doc _and_ "has doc" relation) will be -undone- together if needed
            //....in other words they will behave as if they are a -single- transaction
            var edit_op_rel = edit_op.CreateChainedOperation();
            edit_op_rel.Create(doc_rel_tbl, attribs);
            return edit_op_rel.Execute();
          }
        }
        return false;
      });
    }
    
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also