Programmatically add an activity to the WF designer

Here's a quick code snippet o' the day. If you're hosting the WF designer in an application and want to programmatically add an activity to the designer view, you can use the following code. Basically, grab the IDesignerHost service from your WorkflowView object (the call to GetService below calls the WorkflowView.GetService method and returns the service as an IServiceProvider). Use the IDesignerHost service to get the RootComponent and add your new activity it. For simplicity, we just add a CodeActivity as a child though you can certainly walk the activity tree and insert it wherever you need to. Once this is done, call the AddActivityToDesigner method defined in your WorkflowDesignerLoader object and you're good to go.

public void AddCodeActivityToDesigner()


    WorkflowDesignerLoader loader =

        GetService(typeof(WorkflowDesignerLoader)) as WorkflowDesignerLoader;

    if (loader == null)



    IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));

    CompositeActivity rootActivity = designerHost.RootComponent as CompositeActivity;


    CodeActivity codeActivity = new CodeActivity();