One of the common asks that customers have is how to add/remove activities from the workflow designer during its authoring – automatically though.
Example, If a specific property for a certain activity is changed, we would want that certain activities are added or removed from the workflow in its designer.
How do we go about doing that?
Well, we need to use model item infrastructure to go about it. If you have a custom designer for your custom composite activity, you can override the OnModelItemChanged event. Check the condition you want to base the addition or deletion of the activity and then:
ModelItem myItem = this.ModelItem;
myItem = myItem.Parent;
while (myItem.Parent.ItemType != typeof(Sequence));
where ‘this’ corresponds to the Activity Designer where you have overridden the OnModelItemChaged event.
In the code above, essentially I am getting hold of the model item representing the specific activity and then walking up the tree till I find the root activity. To the root I am saying, can you add another Sequence activity at the last activity in the workflow.
You would notice that immediately a Sequence activity would be added onto your workflow as the specific Model item has changed triggering the event.
Another variation of the above customer ask is, we do not want only one specific activity to be added. We want multiple changes including a few activities added or deleted. Or having a few properties configured specifically when a specific model change happens in the workflow.
We can do that as follows:
using (ModelEditingScope editingScope = this.ModelItem.Root.BeginEdit("Activities"))
In the code above the important thing is that all activities are being added or removed as a single unit of work. Meaning, you can Undo once and all the four Sequence activities added would be removed. Redo once and all of them would be added in a single go. Something like Transaction in the Fx. Our designer equivalent for the same is ModelEditingScope.