I am trying to run a SP2013 workflow from an Event Receiver but I get the error "The request was aborted: The request was canceled. Client ActivityId: aec2fc1b-3f33-44d8-85ab-c6c976617b8c" at the point I actually attempt to start the Workflow.
The scenario is as follows. I have 2 lists: List A and List B. Users have permissions to create items in List A but not List B. When users create a new item in List A, a SP2013 workflow runs which then creates a new item in List B. List B should then
run another SP2103 Workflow but because of how SP and workflows work, this second workflow on List B doesn't automatically run. Thus I have created an Item Added Event Receiver on List B that will run the required workflow automatically (at least it should
if it worked!)
I'm sure the problem must be something to do with permissions but nothing I try seems to solve it:
I can't just use the account of the user triggering the event as this user will be the person adding items to List A and they don't have permissions to do anything in List B (thus they cannot see items in list B or start workflows)
I can't run using RunWithElevatedPrivileges as a) I don't think this works well with Event Receivers and b) SP2103 workflows cannot be run using the admin account.
I have tried impersonation with an account specifically set up for running workflows (named mydomain\wssworkflow). This account has full control of the entire site, lists etc. but it still gives the error mentioned above. My code is shown below - the error
occurs on the line:
wfi.StartWorkflowOnListItem(sub, properties.ListItemId, payload);
Any help in working out why this isn't working would be appreciated.
Thank you,
-Stephen
public override void ItemAdded(SPItemEventProperties properties) { base.ItemAdded(properties); var wsm = new Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager(properties.Web); var wfss = wsm.GetWorkflowSubscriptionService(); var subs = wfss.EnumerateSubscriptionsByList(properties.ListId); foreach (var sub in subs) { if (sub.Name == "RequestAddedToListB") { var userToken = properties.Web.EnsureUser(@"mydomain\wssworkflow").UserToken;
using (SPSite site = new SPSite(properties.Site.ID, userToken)) { using (SPWeb web = site.OpenWeb()) { var payload = new Dictionary<string, object>(); payload.Add("itemId", properties.ListItemId); payload.Add("WorkflowStart", "StartWorkflow"); var wfi = wsm.GetWorkflowInstanceService(); wfi.StartWorkflowOnListItem(sub, properties.ListItemId, payload); break; } } } } }