Workflow will not Initiate Under the System Account

If you have applied MOSS/WSS Service Pack 1 or one of the recent MS Security hot fixes to your environment you may have noticed that workflows once initiated by SharePoint are no longer starting as expected. I came across this issue when developing and testing a timer job which called a SPListItem.Update(). I expected this update to kick off the attached SPD workflow but.. nothing happened. The workflow never started. This was because the timer job was running under the web application pool account which is a SharePoint service account. This issue is outlined in this MS article: http://support.microsoft.com/kb/947284.

The solution supplied in this article was not suited to our needs so the workaround was to impersonate a non-service account when accessing the list and updating the items. A useful resource is this impersonation class which is quite neat because it is instantiated using the using a using-block which allows the tidy release of the resource: http://www.codeproject.com/KB/cs/zetaimpersonator.aspx .

In my case, I added the impersonation account details to a settings database and retrieved these in the timer job via a web service. It is important to note that the impersonation should encompass your SPweb and SPsite using-block code, not just the actual update call. Also, the impersonation account must have the current level of access (at least contribute) to the workflow hosted site and list.

               GetCredentialSettings();
                using (new Impersonator(_username, _domain, _pwd))
                {
                    using (SPSite wfSite = new SPSite(TargetSiteURL))
                    {
                        using (SPWeb wfWeb = wfSite.OpenWeb())
                        {

                            SPList wfList = wfWeb.Lists[TargetListName];
                            foreach (SPListItem item in wfList.Items)
                            {
                                   if(some condition)
                                         item.Update();
                            }
                        }
                  }
               }

Advertisements

One thought on “Workflow will not Initiate Under the System Account

  1. Something like this will trigger the relevant workflows:

    foreach (SPWorkflowAssociation wf in listItem.ParentList.WorkflowAssociations)
    {
    if ( (wf.AutoStartChange || wf.AutoStartCreate) && wf.Enabled)
    {
    _web.Site.WorkflowManager.StartWorkflow(listItem, wf, wf.AssociationData);
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s