Home > Event Receiver, SharePoint 2010 > How to add event receiver to SharePoint2010 content type programmatically

How to add event receiver to SharePoint2010 content type programmatically

Today , I’d like to show how to add event receiver to How to add event receiver to SharePoint2010 content type programmatically.

1. Create empty SharePoint Project and add a class called ItemContentTypeEventReceiver and make it inherit from SPItemEventReceiver and implement your logic as below

public class ItemContentTypeEventReceiver : SPItemEventReceiver
private bool eventFiringEnabledStatus;

public override void ItemAdded(SPItemEventProperties properties)

private void UpdateTitle(SPItemEventProperties properties)
SPListItem addedItem = properties.ListItem;
string enteredTitle = addedItem["Title"] as string;
addedItem["Title"] = enteredTitle + " Updated";

public override void ItemUpdated(SPItemEventProperties properties)

private void DisableItemEventsScope()
eventFiringEnabledStatus = EventFiringEnabled;
EventFiringEnabled = false;

private void EnableItemEventsScope()
eventFiringEnabledStatus = EventFiringEnabled;
EventFiringEnabled = true;

2.Create a Site or Web(depending or your requirements) scoped feature and implement your feature event handler as below:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
SPWeb web = GetFeatureWeb(properties);

string assemblyName =  System.Reflection.Assembly.GetExecutingAssembly().FullName;
const string className = "YBBEST.AddEventReceiverToContentType.ItemContentTypeEventReceiver";
SPContentType contentType= web.ContentTypes["Item"];
AddEventReceiverToContentType(className, contentType, assemblyName, SPEventReceiverType.ItemAdded, SPEventReceiverSynchronization.Asynchronous);
AddEventReceiverToContentType(className, contentType, assemblyName, SPEventReceiverType.ItemUpdated, SPEventReceiverSynchronization.Asynchronous);
protected static void AddEventReceiverToContentType(string className, SPContentType contentType, string assemblyName, SPEventReceiverType eventReceiverType, SPEventReceiverSynchronization eventReceiverSynchronization)
if (className == null) throw new ArgumentNullException("className");
if (contentType == null) throw new ArgumentNullException("contentType");
if (assemblyName == null) throw new ArgumentNullException("assemblyName");
SPEventReceiverDefinition eventReceiver = contentType.EventReceivers.Add();
eventReceiver.Synchronization = eventReceiverSynchronization;
eventReceiver.Type = eventReceiverType;
eventReceiver.Assembly = assemblyName;
eventReceiver.Class = className;

3.Deploy your solution and now you have a event receiver that attached to the Item contentType.

You can download the complete source code here.You can also check how to add event receiver to a list using SharePoint event receiver item in Visual Studio2010 in my previous blog.

  1. Wes Parker
    February 25, 2014 at 9:10 pm

    I downloaded the complete source code, YYBEST.AddEventReceiverToContentType.7z but I don’t know what the file extension is? I’ve tried .cs, .csproj, .txt and .xml. Can you tell me what the file extension should be? Thanks.

  2. Wes Parker
    February 27, 2014 at 2:35 am

    This is Wes Parker again. Can you tell me what the file extension for this download is? It would be nice to actually look at it.

  3. Wes Parker
    February 27, 2014 at 11:44 pm

    I downloaded the 7z executable.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: