Adding Variables, Expressions and Bindings Programmatically

I am assuming we already know how to add activities on to the designer surface dynamically using the designer programmatic APIs. If not please take a look here.

Another variation of the ask is we want to add variables and arguments to our activities/workflows at design time dynamically. Example scenario: If a specific activity is drag dropped and its input type is same as the output of the previous activity on the workflow, bind the input of this newly added activity to the output of the other activity.

Yes, we would need a variable here so that we can bind it to the InArgument and the OutArgument of the activities to link them for data flow.

How do we go about doing it:

 const string myStrName = "myStrName";
Variable<string> mySimpleVar= new Variable<string> { Name = myStrName };

This was just about adding a variable. How about providing a default value to the variable. Lets  see how we go about doing that:

 mySimpleVar.Default = new VisualBasicValue<string>
      ExpressionText = "\"Kushal\"",

Next step/hurdle – What if the variable is of some user defined type? We would then need to add a namespace declaration as part of the Imports section in the Workflow

Example: If you want to create a variable of type fooClass defined in assembly – fooAssembly.dll and namespace - foo, we would go about as follows:

 static string requiredAssemblyName = 

static string requiredNamespace = typeof(foo.fooClass).Namespace;
 VisualBasicSettings vbSettings = new VisualBasicSettings();
vbSettings.ImportReferences.Add(new VisualBasicImportReference
        Assembly = requiredAssemblyName,
        Import = requiredNamespace

mySimpleVar.Default = new VisualBasicValue<foo.fooClass>
       ExpressionText = "new foo.fooClass()",
         Settings = vbSettings


Now the rest is about the binding of the outArgument of one activity to the inArgument of the other activity. For example:

 Argument myArg = 
 myArg.Expression =   new VisualBasicValue<foo.fooClass>
                        ExpressionText = myStrName


where “Response” is an InArgument for my activity and myStrName is the variable we just created above.

We follow the similar model for the outArgument and you have programmatically completed binding the output of one activity to the input of another activity. One change though is that for the outArgument expression assignment you need to use a VisualBasicReference instead of a VisualBasicValue.