SharePoint: accessing the workflow variables after StartWorkflow() has terminated
I am running a SharePoint workflow programmatically from a SharePoint event receiver by calling workflowManager.StartWorkflow().
The workflow is setting some workflow variables during execution. Is there a way to access the last value of these variables after the workflow has terminated (e.g., the call to StartWorkflow() returns)?
Here's my sample code that demonstrates my intent:
public override void ItemAdded(SPItemEventProperties properties)
{
SPWorkflow workflow = null;
SPWorkflowManager workflowManager = null;
try
{
base.ItemAdded(properties);
workflowManager = properties.OpenWeb().Site.WorkflowManager;
var workflowAssociation = properties.ListItem.ParentList.WorkflowAssociations[0];
workflow = workflowManager.StartWorkflow(properties.ListItem, workflowAssociation, "<Data></Data>");
// I can read any fields that were updated by the WF
SPListItem item = properties.ListItem.ParentList.GetItemById(properties.ListItemId);
string validationResult = (string) item["ValidationResult"];
// how can I access any workflow va开发者_运维技巧riables created during execution?
}
catch (Exception ex)
{
Debug.WriteLine(ex);
if (workflow != null && workflowManager != null)
{
SPWorkflowManager.CancelWorkflow(workflow);
}
throw;
}
}
Maybe write them out to a List? This might your least messy option.
Alternatively, there's a SPWorkflow.Xml property that "returns a string that represents the workflow instance in XML format" but I've never tried it out. Though passing this back into SharePoint could be awkward.
精彩评论