Im writing an workflow event receiver that fires on workflowcompleted for a list. If I have two workflows on that list, how can I restrict the completed event to check its happening for the correct workflow?


From SPWorkflowEventProperties you should be able to get the name of the workflow association from which the workflow instance was created.

public override void WorkflowCompleted(SPWorkflowEventProperties properties)
    SPWorkflowActivationProperties activationProperties = properties.ActivationProperties;
    string worflowAssociationName = activationProperties.TemplateName;

    //compare is it for the workflow you want to work with here



It turns out that ActivationProperties is null in WorkflowCompleted event.

A workaround is to use SPWorkflowEventProperties.InstanceId to get the related task and then from the task item get other details, like the related item, the workflow, etc.

private const string WfInstanceQuery = @"<Where><Eq><FieldRef Name='WorkflowInstanceID'/><Value Type='GUID'>{0}</Value></Eq></Where>"; 

SPList wfTaskList = web.Lists["TasksListName"]; 
var items = wfTaskList.GetItems(new SPQuery() 
    Query = string.Format(WfInstanceQuery, properties.InstanceId.ToString()) 

if (items.Count > 0) 
    Guid listId = new Guid(items[0]["WorkflowListId"].ToString()); 
    int itemId = (int)items[0]["WorkflowItemId"]; 
    //Get the List item on which the workflow run 
    SPListItem item = web.Lists[listId].GetItemById(itemId); 
    SPWorkflow workflow = item.Workflows[new Guid(properties.InstanceId.ToString())];
    //check the workflow here


| improve this answer | |
  • ActivationProperties is null in WorkflowCompleted – dmce Jan 28 '14 at 9:17
  • Hmm, try with InstanceId which shouldn't be null. Check this post to get the workflow name through the task item. It may work for your case too. – Naim Murati Jan 28 '14 at 9:45
  • Good. I edited the answer with the reference. – Naim Murati Jan 28 '14 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.