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.