SharePoint aware workflow activities

When creating a custom workflow activity for MOSS 2007 in VS 2005, it would be great to be able to get the current SharePoint web or site that the workflow is attached to. SPContext.Current will not work, as it is null within an activity. If only we could get some sort of context for the workflow…

Enter WorkflowContext (in Microsoft.SharePoint.WorkflowActions namespace, in the DLL of the same name). Unfortunately this is not trivial to access via code – you need to pass it as a parameter to your activity and put in the necessary plumbing code to store it. First you need to setup a DependencyProperty and related property in your activity.

public static DependencyProperty __ContextProperty = 
  DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(MyCustomActivity));

[ValidationOption(ValidationOption.Required)]
public WorkflowContext __Context {
  get { 
    return (WorkflowContext) base.GetValue(__ContextProperty);
  }
  set { base.SetValue(__ContextProperty, value); }
}

You then need to pass the parameter in through the relevant .ACTIONS file:

<Parameter Direction="In"
  Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext, Microsoft.SharePoint.WorkflowActions" 
  Name="__Context" />

You can then use the __Context to access the SharePoint site and web associated with this workflow activity. You can do similar things with the current SharePoint list and list item id. Thanks to these posts for the information!

Comments