Question

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;
    }
Was it helpful?

Solution 2

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

OTHER TIPS

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top