3

I have an event receiver that creates 2 tasks. The task should be created immediately on ItemAdded but there is a delay for some reason. The first task is created immeditately as expected but then there is a 5 minute delay before the second task is created. After that, there is another 15 minute delay before the rest of the Receiver code is executed.

I have isolated it down to the create tasks method. I've seen a lot of documentation on Tasks locking workflows but not much on Event Receivers. Also, I am unable to reproduce this issue in Dev or Stage.

Has anyone seen this issue before?

private int CreateExpiryTasks(SPListItem item, SPEventReceiverType etr) { int tasksAdded = 0; try { SPListItemCollection issueMgmtTasks = item.ParentList.ParentWeb.Lists[issueMgmtTaskList].Items; DateTime today = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); if ((DateTime)item["Recheck Due Date"] >= today) { SPListItem recheckTask = issueMgmtTasks.Add(); recheckTask["Recheck Due Date"] = item["Recheck Due Date"]; recheckTask["Issue"] = item.ID; recheckTask["Title"] = item.Title; recheckTask["Due Date"] = item["Recheck Due Date"]; recheckTask.Update(); tasksAdded++; } if ((DateTime)item["Est Resolution Date"] >= today) { SPListItem estResTask = issueMgmtTasks.Add(); estResTask["Est_x002e__x0020_Resolution_x002"] = item["Est Resolution Date"]; estResTask["Issue"] = item.ID; estResTask["Title"] = item.Title; estResTask["Due Date"] = item["Est Resolution Date"]; estResTask.Update(); tasksAdded++; } } catch (Exception ex) { SPLogger.LogEvent(item.ParentList.ParentWeb, logList, etr, "CreateExpiryTasks: Error: " + ex.Message); } return tasksAdded; } 

2 Answers 2

1

I added Finally in your code. It will allow you to remove the lock used by the eventreceiver in few seconds. As it is the default behavior of SharePoint that if you create an item in Task list than it will put the lock on the item & the list will not be accessible while it is locked.

The code written in the Finally will release the lock from the list & allow you to add/update item in the list.

private int CreateExpiryTasks(SPListItem item, SPEventReceiverType etr) { int tasksAdded = 0; try { SPListItemCollection issueMgmtTasks = item.ParentList.ParentWeb.Lists[issueMgmtTaskList].Items; DateTime today = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); if ((DateTime)item["Recheck Due Date"] >= today) { SPListItem recheckTask = issueMgmtTasks.Add(); recheckTask["Recheck Due Date"] = item["Recheck Due Date"]; recheckTask["Issue"] = item.ID; recheckTask["Title"] = item.Title; recheckTask["Due Date"] = item["Recheck Due Date"]; recheckTask.Update(); tasksAdded++; } if ((DateTime)item["Est Resolution Date"] >= today) { SPListItem estResTask = issueMgmtTasks.Add(); estResTask["Est_x002e__x0020_Resolution_x002"] = item["Est Resolution Date"]; estResTask["Issue"] = item.ID; estResTask["Title"] = item.Title; estResTask["Due Date"] = item["Est Resolution Date"]; estResTask.Update(); tasksAdded++; } } catch (Exception ex) { SPLogger.LogEvent(item.ParentList.ParentWeb, logList, etr, "CreateExpiryTasks: Error: " + ex.Message); } finally { this.EnableEventFiring(); } return tasksAdded; } 

Note:-

My assumption is that, you are calling this function on the ItemUpdated event.

Please refer below links (check the suggested answer in the first link to get reason).

Event receiver running multiple threads

SharePoint Workflow Architecture – Part 3

Execute code in multiple threads (even with SharePoint)

4
  • Is that works for you? Commented Jun 7, 2016 at 15:59
  • I actually ran across the second link there. Pretty good article. It turned out to be some corruption in the Task List. I deleted and recreated the list and issue was resolved. Commented Jun 7, 2016 at 21:54
  • Hmm but the locking will be invoked using my given method in answer. So I gave you the answer for it. Commented Jun 8, 2016 at 14:29
  • Please give some justice to the answer above. As it is also the resolution for the same. Thanks Commented Jun 8, 2016 at 14:32
0

To resolve this issue, I had to delete the task list and recreate.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.