Item Events
Project Automation Item Events allow you to intercept before and after an operation during the sync process.
You can cancel an operation by setting the Sync property to false in the Before event this is the same as unchecking the Sync flag in the compare results window except this allows you to do it programmatically.
- BeforeAddItem(object sender, DataCompareItemInvariant item, object identity)
- AfterAddItem(object sender, DataCompareItemInvariant item, object identity)
- BeforeUpdateItem(object sender, DataCompareItemInvariant item, object identity)
- AfterUpdateItem(object sender, DataCompareItemInvariant item, object identity)
- BeforeDeleteItem(object sender, DataCompareItemInvariant item, object identity)
- AfterDeleteItem(object sender, DataCompareItemInvariant item, object identity)
Not all Data Connectors support Item Events
The item field contains details about the item to be either Added, Updated or Deleted.
- Sync - If the item is to be synchronised.
- Key - The Key value for the item
- Row - A list of Columns for Update
- SourceRow - A list of columns in the source row
- GetTargetIdentifier() - Get the ID value for the Target Item
The identity field may contain the ID value of the target item this depends on the provider implementation.
Clear Sync Flag
If you want to override the Data Sync behaviour and execute your own code you need to set Sync to false in the Before* event. This prevents Data Sync from running the code to write to the target. This is the same as un-checking an item in the change set view.
public override void BeforeAddItem(object sender, DataCompareItemInvariant item, object identity)
{
item.Sync = false;
}
Update Source System
One use-case is where you want to update the source system with the created ID value from the target, for example where in Dynamics CRM you want to add the Entity Guid to your source SQL Table whenever a new account is added. This can be done easily via the UpdateSourceRow helper function on the SQL Data Connector.
public override void AfterAddItem(object sender, DataCompareItemInvariant item, object identity)
{
DataSourceA.UpdateSourceRow("CRM_ID", identity, item);
}
Clear Sync column from Source System
Another Example is using this to clear a Sync column on the source SQL Table after it has been written to the target.
public override void AfterAddItem(object sender, DataCompareItemInvariant item, object identity)
{
DataSourceA.ExecuteNonQuery(string.Format("UPDATE {0} SET [Sync]=0 WHERE [ID]=?", DataSourceA.SourceTable), item.Key);
}
Row Helpers
There are two row helpers which can be used to convert the row data into a Dictionary<string,object> based on the Mapping and change collection. Use the SourceMapping if you want the Dictionary Key to use the source column names or TargetMapping to use the Target column names.
Add Collection
The Add collection includes all the columns from the Source to be written to the target.
public override void BeforeAddItem(object sender, DataCompareItemInvariant item, object identity)
{
var data = item.ToAddItemDictionary(TargetMapping);
}
Update Collection
The Update collection only includes those column which have changed and should be updated on the target.
public override void BeforeUpdateItem(object sender, DataCompareItemInvariant item, object identity)
{
var data = item.ToUpdateItemDictionary(TargetMapping);
}