1

am writing an timer job in SP 2013 on-prem version to manipulate some list items .But there exists already an item event receiver to update some other manipulations . i wanna prevent triggering that event receiver when i do this list update from y custom time rjob. how to achieve this functionality?

           SPSecurity.RunWithElevatedPrivileges(delegate()
            { 
              using (SPSite spSite = new SPSite(siteurl))
              {
                 using (SPWeb paramObjspWeb = spSite.OpenWeb())
                 {
                    try
                    {
                        paramObjspWeb.AllowUnsafeUpdates = true;
                        SPList riltlist = 
                   paramObjspWeb.Lists.TryGetList("Tracker");
                        SPListItem singleitem = riltlist.GetItemById(205);
                        Console.WriteLine(" ID is +++++ " + 
                      singleitem["ID"].ToString());

                     Console.WriteLine(" SI NO is +++++ " + 
                    singleitem["Sl.No."].ToString());

                        Console.WriteLine("project code is +++++ " + 
             singleitem["Project Code"].ToString());
                        singleitem["Project Code"] = " NPR_ELE_DUPS_011_17";
                        singleitem.SystemUpdate();
                    }                           catch { }
                   }                }            });

// here in this line singleitem.SystemUpdate(); it automatically triggering the other item event receiver which i wanna stop.

1

When we are in List Item Event Receiver code, we can modify/update the same item which will fire the event receiver again. For disabling event receiver to get fired again use Disable event firing while updating item. And then enable event firing again after update item.

Create your own Event receiver handling class as follow.

public class EventReceiverManager : SPEventReceiverBase, IDisposable
{
    public EventReceiverManager(bool disableImmediately)
    {
        EventFiringEnabled = !disableImmediately;
    }

    public void StopEventReceiver()
    {
        EventFiringEnabled = false;
    }
    public void StartEventReceiver()
    {
        EventFiringEnabled = true;
    }

    public void Dispose()
    {
        EventFiringEnabled = true;
    }
}

And use this class as below.

using (var eventReceiverManager = new EventReceiverManager(true))
{
    var list = GetList("listName");
    var listItem = list.GetItemById(itemId);
    listItem["field"] = "value";
    listItem.Update();
}

What happens when Event Receiver disabled?

When you disable event receiver, SharePoint internally set a data field in current Thread. So if you can set your current’s thread’s data to the required value before/after updating item, you can control the event receiver. However, setting current thread data manually might be risky. So we can use ‘SPEventReceiverBase’ to control event receiver firing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.