Wednesday, July 9, 2014

Adding event receivers to the list using feature receiver

Step1:
In my case NewsEventReceiver I had taken
Write required events code in event receivers file (ItemAdded, ItemDeleted.....)

Step2:
Add FeatureReceivers file and override the Activated and deactivating methods.

FeatureActivated method
public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = (SPWeb)properties.Feature.Parent;

            string assemblyName = System.Reflection.Assembly.GetExecutingAssembly().FullName;

            //Activate Event Reciever for News
            try
            {
                Logger.LogEntry("Activate Event Reciever for news");
                const string className = "XX.XX.UI.EventHandlers.NewsEventReceiver.NewsEventReceiver";
                SPList newsList = web.GetList(web.ServerRelativeUrl.TrimEnd('/') + Constants.HPNews_ListName);
                AddEventReceiverToList(className, newsList, assemblyName, SPEventReceiverType.ItemAdded, SPEventReceiverSynchronization.Asynchronous);
                AddEventReceiverToList(className, newsList, assemblyName, SPEventReceiverType.ItemUpdated, SPEventReceiverSynchronization.Asynchronous);
                AddEventReceiverToList(className, newsList, assemblyName, SPEventReceiverType.ItemDeleted, SPEventReceiverSynchronization.Asynchronous);

                newsList.Update();
            }
            catch (Exception ex)
            {
                Logger.LogEntry(ex.Message);

            }
}

FeatureDeactivating method

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb web = (SPWeb)properties.Feature.Parent;

            string assemblyName = System.Reflection.Assembly.GetExecutingAssembly().FullName;

            //DeActivate Event Reciever for News
            try
            {
                Logger.LogEntry("DeActivate Event Reciever for news");
                const string className = "XX.XX.UI.EventHandlers.NewsEventReceiver.NewsEventReceiver";
                SPList newsList = web.GetList(web.ServerRelativeUrl.TrimEnd('/') + Constants.HPNews_ListName);
                DeleteEventReceiverToList(className, newsList, assemblyName, SPEventReceiverType.ItemAdded,
                                          SPEventReceiverSynchronization.Asynchronous);


                newsList.Update();
            }
            catch (Exception ex)
            {
                Logger.LogEntry(ex.Message);
            }
}

AddEventReceiverToList method
private void AddEventReceiverToList(string className, SPList list, string assemblyName, SPEventReceiverType eventReceiverType, SPEventReceiverSynchronization eventReceiverSynchronization)
        {
            if (className == null) throw new ArgumentNullException("className");
            if (list == null) throw new ArgumentNullException("list");
            if (assemblyName == null) throw new ArgumentNullException("assemblyName");

            SPEventReceiverDefinition eventReceiver = list.EventReceivers.Add();
            eventReceiver.Synchronization = eventReceiverSynchronization;
            eventReceiver.Type = eventReceiverType;
            eventReceiver.Assembly = assemblyName;
            eventReceiver.Class = className;
            eventReceiver.Update();
        }
     
DeleteEventReceiverToList method
private void DeleteEventReceiverToList(string className, SPList list, string assemblyName, SPEventReceiverType eventReceiverType, SPEventReceiverSynchronization eventReceiverSynchronization)
        {
            if (className == null) throw new ArgumentNullException("className");
            if (list == null) throw new ArgumentNullException("list");
            if (assemblyName == null) throw new ArgumentNullException("assemblyName");

            List<SPEventReceiverDefinition> toRemove = new List<SPEventReceiverDefinition>();
            foreach (SPEventReceiverDefinition d in list.EventReceivers)
            {
                if (d.Class == className)
                {
                    toRemove.Add(d);
                }
            }
            foreach (SPEventReceiverDefinition d2 in toRemove)
            {
                d2.Delete();
            }
        }

No comments:

Post a Comment

Image noise comparison methods

 1. using reference image technique     - peak_signal_noise_ratio (PSNR)     - SSI 2. non-reference image technique     - BRISQUE python pac...