Second Life of a Hungarian SharePoint Geek

August 30, 2013

Deleting items from a list despite event receivers

Filed under: Event receivers, SP 2010 — Tags: , — Peter Holpar @ 02:18

Assume you have a SharePoint list with an ItemDeleting event receiver that cancels deletions, prohibiting users to remove any items even if they have the required permissions. If for some reason you would like to delete an item, you should “inactivate” the event receiver first. Inactivating the event receiver just for the current operation may be not trivial in a production system, as you cannot simply deregister (and then re-register) the event receiver, since your users might start deleting other items in the meantime.

The following code snippet provides a sample console application that performs the deletion even if an event receiver would otherwise cancel the operation. It requires the following command line arguments: URL of the list and the ID of the item that should be deleted.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.SharePoint;
  6.  
  7. namespace DeleteItem
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             try
  14.             {
  15.                 if (args.Length != 2)
  16.                 {
  17.                     throw new ArgumentException("Incorrect number of parameters. Specify List URL and List item ID.");
  18.                 }
  19.                 else
  20.                 {
  21.                     var url = args[0];
  22.                     var itemId = int.Parse(args[1]);
  23.                     using (SPSite site = new SPSite(url))
  24.                     {
  25.                         using (SPWeb web = site.OpenWeb())
  26.                         {
  27.                             var eventFiringHandler = new EventFiringHandler();
  28.                             try
  29.                             {
  30.                                 var list = web.GetList(url.Substring(web.Url.Length));
  31.                                 if (list != null)
  32.                                 {
  33.                                     Console.WriteLine("List '{0}' found.", list.Title);
  34.                                     var item = list.GetItemById(itemId);
  35.                                     Console.WriteLine("Are you sure to delete item '{0}' (y/n)?", item.Title);
  36.                                     var response = Console.ReadLine();
  37.                                     if (response.ToLower() == "y")
  38.                                     {
  39.                                         eventFiringHandler.EventFiringEnabled = false;
  40.                                         item.Delete();
  41.                                         Console.WriteLine("Item '{0}' deleted.", itemId);
  42.                                     }
  43.                                     else
  44.                                     {
  45.                                         Console.WriteLine("Deletion cancelled.");
  46.                                     }
  47.                                 }
  48.                                 else
  49.                                 {
  50.                                     Console.WriteLine("List '{0}' not found.", url);
  51.                                 }
  52.                             }
  53.                             finally
  54.                             {
  55.                                 if (eventFiringHandler != null)
  56.                                 {
  57.                                     eventFiringHandler.EventFiringEnabled = true;
  58.                                 }
  59.                             }
  60.                         }
  61.                     }
  62.                 }
  63.             }
  64.             catch (Exception ex)
  65.             {
  66.                 Console.WriteLine("Exception: {0}\r\n{1}", ex.Message, ex.StackTrace);
  67.             }
  68.         }
  69.  
  70.         internal class EventFiringHandler : SPItemEventReceiver
  71.         {
  72.             public bool EventFiringEnabled
  73.             {
  74.                 get
  75.                 {
  76.                     return base.EventFiringEnabled;
  77.                 }
  78.                 set
  79.                 {
  80.                     base.EventFiringEnabled = value;
  81.                 }
  82.             }
  83.         }
  84.     }
  85. }

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: