Programmatically Disable Event Firing on List Item Update in SharePoint 2010

Introduction

In this article you will see how to disable event firing on a list item update using the SharePoint Object Model. I have a custom list in which an Item updated event receiver is implemented. Whenever an item is updated an email will be sent. But I want to update a few column values without firing the item updated event receiver. The following procedure needs to be followed to disable event firing on a list item update.

Create a Console Application using the following:

  1. Open Visual Studio 2010 by going to Start | All Programs | Microsoft Visual Studio 2010 | Right-click on Microsoft Visual Studio 2010 and click on Run as administrator.
  2. Go to the File tab, click on New and then click on Project.
  3. In the New Project dialog box, expand the Visual C# node, and then select the Windows node.
  4. In the Templates pane, select Console Application.
  5. Enter the Name as DisableEventFiring and then click OK.
  6. In the Solution Explorer, right-click on the solution and then click on Properties.
  7. Select the Application tab; check whether ".Net Framework 3.5" is selected for Target Framework.
  8. Select the Build tab; check whether "Any CPU" is selected for Platform Target.
  9. In the Solution Explorer, right-click on the References folder and click on Add Reference.
  10. Add the following reference.

    1. Microsoft.SharePoint.dll

Create EventFiring.cs

  1. Right-click on the project, select Add and click on New Item.
  2. In the templates pane, select Class.
  3. Enter the Name as EventFiring and then click OK.
  4. Replace EventFiring.cs with the following code:
     

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using Microsoft.SharePoint;

     

    namespace DisableEventFiring

    {

       public classEventFiring : SPItemEventReceiver

        {

           public void DisableHandleEventFiring()

            {

               this.EventFiringEnabled =false;

            }

     

           public void EnableHandleEventFiring()

            {

               this.EventFiringEnabled =true;

            }

        }

    }

Program.cs

  1. Replace Program.cs with the following code:
     

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using Microsoft.SharePoint;

     

    namespace DisableEventFiring

    {

       class Program

        {

           static void Main(string[] args)

            {

               using (SPSite site = new SPSite("https://serverName/sites/Vijai/"))

                {

                    using (SPWeb web = site.OpenWeb())

                    {

                        SPList list = web.Lists.TryGetList("Custom");

                        SPListItem item = list.GetItemById(34);

                        item["Title"] ="Updated Successfully";

                        EventFiring eventFiring = newEventFiring();

                        eventFiring.DisableHandleEventFiring();

                        item.Update();

                        eventFiring.EnableHandleEventFiring();

                        Console.WriteLine("Updated Successfully");

                        Console.ReadLine();

                    }

                }

            }

        }

    }
     
  2. Hit F5.
  3. The item is updated successfully without firing the item updated event receiver.

Summary

Thus in this article you have seen how to disable event firing on a list item update using the SharePoint Object Model.

Up Next
    Ebook Download
    View all
    Learn
    View all