Freigeben über


Adding Activities on the designer programmatically

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;
do
{
    myItem = myItem.Parent;
}
while (myItem.Parent.ItemType != typeof(Sequence));

myItem.Parent.Properties["Activities"].Collection.Add(new 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"))
 {
  this.ModelItem.Root.Properties["Activities"].Collection.Add(new Sequence());
  this.ModelItem.Root.Properties["Activities"].Collection.Add(new Sequence());
  this.ModelItem.Root.Properties["Activities"].Collection.Add(new Sequence());
  this.ModelItem.Root.Properties["Activities"].Collection.Add(new Sequence());

 editingScope.Complete();

 }
  

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.

 

Thanks,

Kushal.

Comments

  • Anonymous
    October 08, 2014
    Hello Kushal, thank you for your nice post.It helped me when trying to add activities, but i have one question regarding the deletion of activities. I am using a WF4 rehosted desginer. I have a Flowchart with 4 activities at first, and i want to select three of them by pressing CTRL and then pres delete. At this moment my ModelChanged event handler is coming to work and my issue appears also. I want to get a list of those three selected activities, is that possible ? From what i've done until now, i am able to just get the Flowchart containing those activities. I am not able to go straight to them.