11 Jun 2010 18:08
This is just a quick one prompted by a question on the SSIS Forum, how to programmatically add a precedence constraint (aka workflow) between two tasks.
To keep the code simple I’ve actually used two Sequence containers which are often used as anchor points for a constraint. Very often this is when you have task that you wish to conditionally execute based on an expression. If it the first or only task in the package you need somewhere to anchor the constraint too, so you can then set the expression on it and control the flow of execution.
Anyway, back to my code sample, here’s a quick screenshot of the finished article:
Now for the code, which is actually pretty simple and hopefully the comments should explain exactly what is going on.
Package package = new Package();
package.Name = "SequenceWorkflow";
// Add the two sequence containers to provide anchor points for the constraint
// If you use tasks, it follows exactly the same pattern, they all derive from Executable
Sequence sequence1 = package.Executables.Add("STOCK:Sequence") as Sequence;
sequence1.Name = "SEQ Start";
Sequence sequence2 = package.Executables.Add("STOCK:Sequence") as Sequence;
sequence2.Name = "SEQ End";
// Add the precedence constraint, here we use the package's constraint collection
// as it hosts the two objects we want to constrain (link)
// The default constraint is a basic On Success constraint just like in the designer
PrecedenceConstraint constraint = package.PrecedenceConstraints.Add(sequence1, sequence2);
// Change the settings to use a (dummy) expression only
constraint.EvalOp = DTSPrecedenceEvalOp.Expression;
constraint.Expression = "1 == 1";
The complete code file is available to download below.