Monday, January 14, 2013

Workflow not running when item updated from event receiver.



I finally found the post to solve my problem of having to run workflow from SPItem.Update()

"

workflow's and event handlers run on a different thread than the main thread. You just cannot create another Console/Windows Application and have it trigger the workflow and just quit.
Quitting the application before the asyc worker threads finish their work, causes those threads to simply abort. In case of workflow, it would seem as workflow never fired !!
So what is the fix here? Use the following line of code
SPSite.WorkflowManager.Dispose();

after updating the item. So effectively the overall code becomes:
using(SPSite site = new SPSite(url))
{
    using(SPWeb web = site.OpenWeb())
    {
        SPList list = web.GetList(url);

        SPListItem item = list.Items[1];
        item[updateField] = DateTime.Now.ToLongDateString();
        item.Update();
        site.WorkflowManager.Dispose();
    }
}
Now when I ran the console application, as expected the item updates and the workflow is triggered 

"
Source: http://blogs.msdn.com/b/malag/archive/2008/07/11/splistitem-update-not-starting-workflow.aspx

No comments:

Post a Comment