1

I have a SharePoint 2013 site with multiple lists. Many of the lists have a common content type. We have created custom event receivers to be attached to these lists. But since we want the same event receivers to be attached to these multiple lists, we were attaching it to the content type and not explicitly to a specific list.

So, we removed the ListTemplateID and ListUrl attributes from the Receivers tag in the xml. But because of this, I observed through SharePoint Manager that these event receivers are getting registered at the site level (same the scope of the project containing the event receivers), instead of getting attached to the content types / lists and hence they are firing for every list. This behaviour is, as per the MSDN documentation. But now, I have the following questions.

  1. Is it possible to attach a common event receiver to a set of lists (not based on Template ID) or content types?

  2. When we simply try to attach the event receivers to the content type, is it possible to ensure that the event receiver fires only for the intended lists/content types and not for every list in the site?

  • I guess answer to your both question is NO as per me. What alternate you can do is create another custom content type and use that in the list for which you need event receiver and attache event receiver to newly crated content type. – Bhaskar Dhone Jan 11 '16 at 9:10
3
  1. Declaratively (with elements.xml), you can attach an event receiver (ER) to a content type (CT), meaning all items in all lists of the site based on this CT will have the ER. This can be done from the CT declaration XML itself, see for instance Event receiver attached to content type via XmlDocuments ignored? or the XML block in this MSDN article.

  2. But my preferred method is to attach ER from a feature event-receiver.
    The feature ER is a custom code you write and that runs when the feature is activated. At this point, you can access, with C# code, any object you like (existing CT, list instances...) and attach programatically your list ER to it. See my answer to get a generic C# method I wrote and often use to attach ER to lists, can be easily adapted for CT:

    public static void EnsureER(SPContentType ct, string name, SPEventReceiverType type) where T : SPItemEventReceiver
    {
    // We get the assembly-class names from the T generic type
    string assemblyName = typeof(T).Assembly.FullName;
    string className = typeof(T).FullName;
    // We first remove any existing def matching the same criterias
    List defToRemove = new List();
    SPEventReceiverDefinitionCollection erCollection = ct.EventReceivers;
    foreach (SPEventReceiverDefinition def in erCollection)
    {
        if ((def.Class == className) && (def.Assembly == assemblyName) && (def.Type == type))
            defToRemove.Add(def);
    }
    foreach (SPEventReceiverDefinition def in defToRemove)
        def.Delete();
    SPEventReceiverDefinition newDef = erCollection.Add();
    newDef.Assembly = assemblyName;
    newDef.Class = className;
    newDef.Name = name;
    newDef.Type = type;
    newDef.SequenceNumber = 1000;
    newDef.Update();
    }
    

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.