Attaching / Removing a Receiver using a Feature

namespace TheNameSpace
{
    class Installer: SPFeatureReceiver
    {
        const string asmName = "AssemblyName, Version=1.0.0.0, Culture=neutral,  PublicKeyToken=219b9c8fbe0d2ac7";

        const string itemReceiverName = "AssemblyName.Reciever";

        const string targetlist = "Target List";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWeb oWeb = (SPWeb) properties.Feature.Parent;

                if (!Exists(oWeb, targetlist))
                {
                    SPList oList = oWeb.Lists[targetlist];

                    oList.EventReceivers.Add(SPEventReceiverType.ItemAdded, asmName, itemReceiverName);

                    oList.EventReceivers.Add(SPEventReceiverType.ItemUpdated, asmName, itemReceiverName);

                    oList.EventReceivers.Add(SPEventReceiverType.ItemDeleting, asmName, itemReceiverName);

                    oList.Update();
                    oWeb.Update();
                }
                else
                {
                    throw new Exception(String.Format("The has been an error "
                    + "binding to the list {0} as it does not appear to "
                    + "be present.", targetlist));
                }
            }
            catch (Exception e)
            {
                throw new Exception(String.Format("The has been an error "
                + "binding the Event to the {1} list {0}.", e, targetlist));
            }
        }

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

            if (Exists(oWeb, stafflist))
            {
                SPList oList = oWeb.Lists[stafflist];

                SPEventReceiverDefinitionCollection oCurrentRecievers = oList.EventReceivers;

                List < SPEventReceiverDefinition > oRecieversToDelete = new List < SPEventReceiverDefinition > ();

                foreach(SPEventReceiverDefinition oReciever in oCurrentRecievers)
                {
                    if (oReciever != null && oReciever.Assembly.Equals(asmName))
                    {
                        oRecieversToDelete.Add(oReciever);
                    }
                }
                foreach(SPEventReceiverDefinition oReciever in oRecieversToDelete)
                {
                    try
                    {
                        oReciever.Delete();
                    }
                    catch (Exception e)
                    {
                        //will throw to and error page
                        throw new Exception(String.Format("The has been an error de-binding the Event"
                        + "and/or removing the fields from the {1} list {0}.", e, targetlist));
                    }
                }
                oList.Update();
                oWeb.Update();
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            //do nothing
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            //do nothing
        }

        private bool Exists(SPWeb oweb, string listname)
        {
            using(SPWeb oWeb = oweb)
            {
                foreach(SPList oList in oWeb.Lists)
                {
                    if (oList.Title.Equals(listname))
                    {
                        return (true);
                    }
                }
                return (false);
            }
        }
    }
}

Leave a Reply