Advertisement
private775

C#/SP: add/remove list event receiver

May 12th, 2016
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1.         private const string ReqListName = "Requests";
  2.  
  3.         // Uncomment the method below to handle the event raised after a feature has been activated.
  4.         public override void FeatureActivated(SPFeatureReceiverProperties properties)
  5.         {
  6.             addListEventReceivers(properties, ReqListName, typeof(RequestEventReceiver), SPEventReceiverType.ItemAdded);
  7.         }
  8.  
  9.         // Uncomment the method below to handle the event raised before a feature is deactivated.
  10.         public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
  11.         {
  12.             removeListEventReceivers(properties, ReqListName, typeof(RequestEventReceiver), SPEventReceiverType.ItemAdded);
  13.         }
  14.  
  15.  
  16.         private void addListEventReceivers(SPFeatureReceiverProperties properties, string listName, Type eventReceiverClassType, SPEventReceiverType eventReceiverType)
  17.         {
  18.             try
  19.             {
  20.                 SPWeb w = properties.Feature.Parent as SPWeb;
  21.                 if (w != null)
  22.                 {
  23.                     SPList l = w.Lists.TryGetList(listName);
  24.                     if (l != null)
  25.                     {
  26.                         l.EventReceivers.Add(eventReceiverType, eventReceiverClassType.Assembly.FullName, eventReceiverClassType.FullName);
  27.                     }
  28.                 }
  29.             }
  30.             catch (Exception ex)
  31.             {
  32.                 CustomSpLoggingService.Current.LogException(CustomSpLoggingService.DiagnosticCategory.FeatureActivation, ex);
  33.                 throw;
  34.             }
  35.         }
  36.  
  37.  
  38.         private void removeListEventReceivers(SPFeatureReceiverProperties properties, string listName, Type eventReceiverClassType, SPEventReceiverType eventReceiverType)
  39.         {
  40.             try
  41.             {
  42.                 SPWeb w = properties.Feature.Parent as SPWeb;
  43.                 if (w != null)
  44.                 {
  45.                     SPList l = w.Lists.TryGetList(listName);
  46.                     if (l != null)
  47.                     {
  48.                         if (l.EventReceivers != null && l.EventReceivers.Count > 0)
  49.                         {
  50.                             SPEventReceiverDefinition er = l.EventReceivers.Cast<SPEventReceiverDefinition>().SingleOrDefault(
  51.                                 x => x.Assembly == eventReceiverClassType.Assembly.FullName &&
  52.                                         x.Class == eventReceiverClassType.FullName &&
  53.                                         x.Type == eventReceiverType);
  54.                             if (er != null)
  55.                             {
  56.                                 er.Delete();
  57.                             }
  58.                         }
  59.                     }
  60.                 }
  61.             }
  62.             catch (Exception ex)
  63.             {
  64.                 CustomSpLoggingService.Current.LogException(CustomSpLoggingService.DiagnosticCategory.FeatureActivation, ex);
  65.                 throw;
  66.             }
  67.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement