1

I am trying to add an ItemAdded event receiver in a FeatureActivated event receiver (because it's a site scoped feature and apparently that means SharePoint will ignore anything I tell it and apply it to all lists on the site if I use XML to register it). So inside the FeatureActivated method in the Feature event receiver, I've added:

SPList list = web.GetList("/Lists/EmailQueue");
string assemblyName = typeof(EmailAddedEventReceiver.EmailAddedEventReceiver).Assembly.GetName().FullName;
string className = typeof(EmailAddedEventReceiver.EmailAddedEventReceiver).FullName;
list.EventReceivers.Add(SPEventReceiverType.ItemAdded, assemblyName, className);

I've debugged through it and the assembly and class name both look correct, and the count property of list.EventReceivers goes from 0 to 1. But when I try actually adding things to the list the receiver doesn't fire, and when I go into powershell and check for event receivers on lists it doesn't show any receiver registered to the list.

I have also tried adding list.update() at the end, but that didn't help, as well as resetting OWSTIMER between deploys. I'm not even sure what else to try, or how to figure out what the error is.

0

Try this

using System.Reflection;

const string _listName = "EmailQueue";
const SPEventReceiverType _eventType = SPEventReceiverType.ItemAdded;

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb oWeb = (SPWeb)properties.Feature.Parent;
    //The last parameter of the Add() function can be found in the elements.xml file of the EventReceiver1 project.
    //<Class>EventReceiverProject1.EventReceiver1.EventReceiver1</Class>
    oWeb.Lists[_listName].EventReceivers.Add(_eventType, Assembly.GetExecutingAssembly().FullName, "EventReceiverProject1.EventReceiver1.EventReceiver1");
}

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

    SPList oList = oWeb.Lists[_listName];
    for (int i = oList.EventReceivers.Count - 1; i >=0 ; i--)
    {
        if (oList.EventReceivers[i].Type.Equals(_eventType))
        {
            try
            {
                oList.EventReceivers[i].Delete();
            }
            catch (Exception e)
            {
                // Write to logs
            }
        }
    }
}
  • Hi, What is the difference from the earlier code to your solution ? – Arvi Oct 24 '14 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.