Navigation

Saturday, 7 March 2015

Programmatically Disable Event Receiver Firing on List Item in SharePoint

Through this post, you will see how we will disable Event Firing for SP List Item for a Custom List.

Step:-1 Add a class file in your project. Let's name it clsEnableDisableEvent.cs

Step:-2 Put below code in above class file...


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace YourProjectNamespace
{
   public class clsEnableDisableEventSPItemEventReceiver
    {
        public void DisableListItemEventFiring()
        {
           this.EventFiringEnabled =false;
        }

        public void EnableListItemEventFiring()
        {
           this.EventFiringEnabled =true;
        }
    }
}

Step:-3 Now use above create class in your Web Part.



using (SPSite site = new SPSite("Your SharePoint Site Url"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["YourCustomList"];
                    SPListItem item = list.GetItemById(1);
                    item["Title"] ="New Updated Title";

                    clsEnableDisableEvent objItemEvent = new clsEnableDisableEvent();
                    objItemEvent.DisableListItemEventFiring();
                    item.Update();

                    objItemEvent.EnableListItemEventFiring();                   
                }

            }

Run your code. Now when you update/Delete list item Event Receiver will not fire.  

No comments:

Post a Comment