Miałem dzisiaj dość pisania za każdym razem tego samego kodu, więc napisałem sobie coś takiego:
namespace Gutek.SharePoint.Common.Extensions { public static class SPListEx { public static void Register<T>(this SPList list, params SPEventReceiverType[] receiverTypes) where T : SPItemEventReceiver { if (receiverTypes == null || receiverTypes.Length == 0) { return; } Type type = typeof(T); string assemblyFullName = type.Assembly.FullName; string typeFullName = type.FullName; foreach (var receiverType in receiverTypes) { list.EventReceivers.Add(receiverType, assemblyFullName, typeFullName); } list.Update(); } public static void Unregister<T>(this SPList list, params SPEventReceiverType[] receiverTypes) where T : SPItemEventReceiver { if (receiverTypes == null || receiverTypes.Length == 0) { return; } var toBeDeleted = new List<Guid>(); Type type = typeof(T); string assemblyFullName = type.Assembly.FullName; string typeFullName = type.FullName; foreach (SPEventReceiverDefinition eventReceiverDef in list.EventReceivers) { foreach (var receiverType in receiverTypes) { if (eventReceiverDef.Type == receiverType) { if (eventReceiverDef.Assembly == assemblyFullName && eventReceiverDef.Name == typeFullName) toBeDeleted.Add(eventReceiverDef.Id); } } } foreach (var id in toBeDeleted) { list.EventReceivers[id].Delete(); } list.Update(); } } }
Kożystam z params ze względu na to iż lubie podawać rzeczy po przecinku a nie pisać new [] {1,2}.
Teraz robimy tak i kłopot z głowy :)
SPContext.Current.List.Register<MyItemEventReceiver>(SPEventReceiverType.ItemAdded, SPEventReceiverType.ItemUpdated);
Hej,
Świetne metody. To jest przykład jak lenistwo może pomóc ;)
dzieki :) a tak szczerze to takich metod jest masa :) zobacz najnowszy SPG, maja fajnego helpera do WebConfig Modification.
Comments are closed.