private const string ReqListName = "Requests";
// Uncomment the method below to handle the event raised after a feature has been activated.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
addListEventReceivers(properties, ReqListName, typeof(RequestEventReceiver), SPEventReceiverType.ItemAdded);
}
// Uncomment the method below to handle the event raised before a feature is deactivated.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
removeListEventReceivers(properties, ReqListName, typeof(RequestEventReceiver), SPEventReceiverType.ItemAdded);
}
private void addListEventReceivers(SPFeatureReceiverProperties properties, string listName, Type eventReceiverClassType, SPEventReceiverType eventReceiverType)
{
try
{
SPWeb w = properties.Feature.Parent as SPWeb;
if (w != null)
{
SPList l = w.Lists.TryGetList(listName);
if (l != null)
{
l.EventReceivers.Add(eventReceiverType, eventReceiverClassType.Assembly.FullName, eventReceiverClassType.FullName);
}
}
}
catch (Exception ex)
{
CustomSpLoggingService.Current.LogException(CustomSpLoggingService.DiagnosticCategory.FeatureActivation, ex);
throw;
}
}
private void removeListEventReceivers(SPFeatureReceiverProperties properties, string listName, Type eventReceiverClassType, SPEventReceiverType eventReceiverType)
{
try
{
SPWeb w = properties.Feature.Parent as SPWeb;
if (w != null)
{
SPList l = w.Lists.TryGetList(listName);
if (l != null)
{
if (l.EventReceivers != null && l.EventReceivers.Count > 0)
{
SPEventReceiverDefinition er = l.EventReceivers.Cast<SPEventReceiverDefinition>().SingleOrDefault(
x => x.Assembly == eventReceiverClassType.Assembly.FullName &&
x.Class == eventReceiverClassType.FullName &&
x.Type == eventReceiverType);
if (er != null)
{
er.Delete();
}
}
}
}
}
catch (Exception ex)
{
CustomSpLoggingService.Current.LogException(CustomSpLoggingService.DiagnosticCategory.FeatureActivation, ex);
throw;
}
}
Comments