Sunday 12 January 2014

Add and Remove Event Receiver through Feature SharePoint 2010

1. Add Event Receiver using Visual Studio( Here, I added list leve ItemUpdated event receiver
     Solution -->Add -->Add New Item


2.Add feature reciever and add  the below code

public class FeatureEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb spWeb = properties.Feature.Parent as SPWeb;
            try
            {
                using (SPSite siteCollection = new SPSite(spWeb.Url))
                {
                    using (SPWeb web = siteCollection.OpenWeb())
                    {
                        SPList list = web.Lists["ListName"];
                        SPEventReceiverDefinitionCollection spEventReceiverDefinitionCollection = list.EventReceivers;
                        if (!isEventAlreadyAttached(spEventReceiverDefinitionCollection, "AssemblyName"))
                        {
                            SPEventReceiverType spEventReceiverType = SPEventReceiverType.ItemUpdated;
                            spEventReceiverDefinitionCollection.Add(spEventReceiverType, " AssemblyName ", "EventReceiverClassName");
                            list.Update();
                            web.Update();
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                //Log you message
            }
            finally
            {
                spWeb.Dispose();
            }
          
        }
        private bool isEventAlreadyAttached(SPEventReceiverDefinitionCollection spEventReceiverDefinitionCollection, string myAssembly)
        {
            bool eventReceiverAttached = false;
            foreach (SPEventReceiverDefinition spEventReceiverDefinition in spEventReceiverDefinitionCollection)
            {
                if (spEventReceiverDefinition.Assembly.Contains(myAssembly))
                {
                    eventReceiverAttached = true;
                    break;
                }
            }
            return eventReceiverAttached;
        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
           
            SPWeb spWeb = properties.Feature.Parent as SPWeb;
            try
            {
                RemoveEventReceiver(spWeb.Url);
              
            }
            catch
            {
                return;
            }
            finally
            {
                spWeb.Dispose();
            }
        }

        private void RemoveEventReceiver(string siteUrl)
        {
            using (SPSite siteCollection = new SPSite(siteUrl))
            {
                using (SPWeb web = siteCollection.OpenWeb())
                {
                    List<Guid> receiversToRemove = new List<Guid>();
                    SPList list = web.Lists["ListName"];
                    SPEventReceiverDefinitionCollection spEventReceiverDefinitionCollection = list.EventReceivers;
                    for (int i = 0; i < spEventReceiverDefinitionCollection.Count; i++)
                    {
                        if (spEventReceiverDefinitionCollection[i].Assembly.Contains("AssemblyName"))
                        {
                            receiversToRemove.Add(spEventReceiverDefinitionCollection[i].Id);
                        }
                    }
                    if (receiversToRemove.Count > 0)
                    {
                        foreach (var guid in receiversToRemove)
                        {
                            list.EventReceivers[guid].Delete();

                        }
                        list.Update();
                        web.Update();
                    }
                }
            }
        }
    }

Note: Replace ListName and AssemblyName details.


No comments:

Post a Comment