Targeting Event Receivers To Content Types

I was recently trying to target an event receiver that overrode the ItemUpdated event to a specific base content type, which, as I found out, is not easy to do, in the realm that building an event receiver that deploys via a SharePoint feature, you use the templateID and optionally use the ListTemplateOwner attribute.


name, Version=,  Culture=neutral, PublicKeyToken=*******************



Well, this isn’t very good, because I want to target a base content type, not a list template, or pass in a list ID to target even, that is entirely too limiting IMHO. I would have thought that there would have been a ContentType attribute for targeting with the Receivers element (I mean, come on, the freakin SPContentType object has an EventReceivers collection, as you will see in the below code), however I guess I was wrong, which is stupid IMHO by MSFT when designing receiver deployments since receivers are such much easier to write than the throwback event sinks that were offered in V2 which makes them attractive for trapping behavior. To get around this problem, I just use a feature receiver to associate my event receiver with a content type, when the event receiver is deployed. So, the receiver is just associated with the content type on Feature activation. I really find this solution frustrating, however it works fine, and it gets the job done.


using Microsoft.SharePoint;
using System.Reflection;

namespace exampleContentTypeActivation
public class exampleFeatureReceiver : SPFeatureReceiver

public override void FeatureInstalled(SPFeatureReceiverProperties properties) { }
public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { }
public override void FeatureDeactivating(SPFeatureReceiverProperties properties){ }
public override void FeatureActivated(SPFeatureReceiverProperties properties)
using (SPSite siteCollection = (SPSite) properties.Feature.Parent)
using (SPWeb site = siteCollection.OpenWeb())
SPContentType contentType = site.AvailableContentTypes[“Document”];
Assembly assembly = Assembly.GetExecutingAssembly();
SPEventReceiverDefinition reciever = contentType.EventReceivers.Add();
reciever.Name = “example_Name”;
reciever.Type = SPEventReceiverType.ItemUpdated;
reciever.SequenceNumber = 10000;
reciever.Assembly = string.Format(“{0},{1},{2},{3}”, assembly.GetName().FullName, assembly.GetName().Version, assembly.GetName().CultureInfo, assembly.GetName().GetPublicKey());
reciever.Class = “example_Class_Name”;


Ok, so in the above code, I am trying to keep this as dynamic as possible by reading the various assembly attributes and constructing them within the Feature receiver code, since I am compiling both the event receiver class and the Feature receiver class within the same assembly. Therefore, we can just use System.Reflection in order to get a lot of the attributes that are required in order to construct the receiver code, since we just need things like the culture info through the CultureInfo property and the Public Key token using the GetPublicKey() token method, etc. etc. etc.
That being said, I find not having this functionality where you can target a specific content directly in an element, without using such a bullshit workaround very, very silly. I think that there should be within the Receivers element an option to target a content type with its own attribute, as both of these pieces of technology were something that I was very excited about with the V3. Not making them marry when I want them to is straight up stupid.


7 thoughts on “Targeting Event Receivers To Content Types”

  1. I need to write an event for mouse hover or onclick on document library to display some message.

    Any advise?

  2. Hi Adam,

    I agree with you on this … they made something that should be simple really really difficult.

    I’ve tried your methods above in a VS 2005 project but it doesn’t associate the Feature only with the Content Type. It associates with a ListTemplateId also. In the MSFT documentation it says that ListTemplateId is OPTIONAL but in fact if you try to activate without the ListTemplateId in the node then it will error out.

    I don’t have a solution for this yet. I really need to activate only for a content type.

    Any ideas?

  3. Let me say the code above is not working… in order to make it right you need to set Assembly properly meaning
    “AssemblyFullName, Version=, Culture=neutral, PublicKeyToken=XXX” – you miss keywords Version, Culture etc.

  4. I spent several hours trying to get this code to work. Bottom line is you’re code is incorrect in two places.
    The collection returned from “site.AvailableContentTypes” is readonly, use “site.ContentTypes”
    You must include contentType(true) at the end. The “true” is a must for this to work on List content types (which I have)

  5. hi,

    How do i use the above solution, i cant make it work for a web, how should i create the object for this receiver class to invoke activation.


  6. hi,

    In my scenario web.ContentTypes returns count as zero, can you suggest if there is a flag i need to enable?


Leave a Reply

Your email address will not be published. Required fields are marked *