Second Life of a Hungarian SharePoint Geek

August 6, 2010

Event receiver “inheritance” between parent and child content types

Filed under: Content types, Event receivers, SP 2010 — Tags: , , — Peter Holpar @ 23:02

On the week I got a task for creating a simple routing workflow. Forms including different pieces of information are submitted by users and this data must be approved by another users. The workflow itself is not important here, the main point is that the data submitted is different but the workflow should be common.

We already solved similar workflows utilizing item event receiver, but in that cases the receiver was bound to the list. Now I planned that the workflow would be associated with a parent content type that would contain only the fields required by the workflow, and child content types would be “inherited” from the parent to add form data.

It sounds good, but to tell the truth I’ve never tried that before and found no information on the web (except this MSDN forum thread after my tests) about whether the event receiver settings would be propagated to child content types or not.

So, I’ve created a simple event receiver that only trace out the content type information of the new item when it was added to the list:

  1. using System;
  2. using System.Security.Permissions;
  3. using Microsoft.SharePoint;
  4. using System.Diagnostics;
  6. namespace SimpleEventReceiver
  7. {
  8.     /// <summary>
  9.     /// List Item Events
  10.     /// </summary>
  11.     public class SimpleEventReceiver : SPItemEventReceiver
  12.     {
  13.        /// <summary>
  14.        /// An item was added.
  15.        /// </summary>
  16.        public override void ItemAdded(SPItemEventProperties properties)
  17.        {
  18.            Trace.TraceInformation("Content type: {0}", properties.ListItem.ContentType.Name);           
  19.            base.ItemAdded(properties);
  20.        }
  22.     }
  23. }

Next, I’ve created a new content type called EventReceiverParent on the UI derived from the Item parent content type, and registered the event receiver using a simple command line tool.

RegisterContentTypeReceiver(web, "EventReceiverParent");

Where the RegisterContentTypeReceiver method looks like this:

  1. private void RegisterContentTypeReceiver(SPWeb web, String contentTypeName)
  2.         {
  3.             Console.WriteLine("Registering: {0}", contentTypeName);
  4.             SPContentType contentType = web.ContentTypes[contentTypeName];
  6.             SPEventReceiverDefinition eventReceiverDefinition = contentType.EventReceivers.Add();
  7.             eventReceiverDefinition.Type = SPEventReceiverType.ItemAdded;
  8.             eventReceiverDefinition.Assembly = "SimpleEventReceiver, Version=, Culture=neutral, PublicKeyToken=624649cd872eaad1, processorArchitecture=MSIL";
  9.             eventReceiverDefinition.Class = "SimpleEventReceiver.SimpleEventReceiver";
  10.             eventReceiverDefinition.SequenceNumber = 1000;
  12.             eventReceiverDefinition.Update();
  13.             // use the following if you would like only new child content types to inherit setting
  14.             // but not existing ones:
  15.             //contentType.Update();
  16.             contentType.Update(true);
  18.             DisplayContentTypeReceivers(web, contentTypeName);
  19.         }

The DisplayContentTypeReceivers displays the event receiver registration for the specified content type:

  1. private void DisplayContentTypeReceivers(SPWeb web, String contentTypeName)
  2. {
  3.     Console.WriteLine("Display: {0}", contentTypeName);
  4.     SPContentType contentType = web.ContentTypes[contentTypeName];
  5.     foreach (SPEventReceiverDefinition eventReceiverDefinition in contentType.EventReceivers)
  6.     {
  7.         Console.WriteLine("Assembly: '{0}', Class: {1}, SequenceNumber: {2}",
  8.             eventReceiverDefinition.Assembly,
  9.             eventReceiverDefinition.Class,
  10.             eventReceiverDefinition.SequenceNumber);
  11.     }
  12. }

After this, I’ve created a child content type called EventReceiverChild on the UI, and displayed the event receivers for that:

DisplayContentTypeReceivers(web, "EventReceiverChild");

The result showed that the receiver information was inherited to the child content type.

I found the same, when created another child content type called called EventReceiverChild2 using code:

AddContentType(web, "EventReceiverParent", "EventReceiverChild2");

Where the AddContentType method looks like this:

  1. private void AddContentType(SPWeb web, String parentContentTypeName, String childContentTypeName)
  2. {
  3.     Console.WriteLine("Creating {0} based on {1}", childContentTypeName, parentContentTypeName);
  4.     SPContentType childContentType = new SPContentType(web.ContentTypes[parentContentTypeName], web.ContentTypes, childContentTypeName);
  5.     web.ContentTypes.Add(childContentType);
  7.     DisplayContentTypeReceivers(web, childContentTypeName);
  8. }

When you register a new event receiver for the parent content type, and update the content type using the Update() or Update(false) method instead of Update(true), then I found that the changes are not propagated down to the children content types.

When I assigned the content types to a list, and created new items for each of the content types, I found that the event receiver is triggered for each and the name of the right content type is traced out.

The result of my tests shows that utilizing an event receiver bound to a parent content type is a practical way to create simple workflows for children content types.

Note, that I made the tests on SharePoint 2010. There might be differences when you work on WSS 3.0 / MOSS 2007.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

You are commenting using your 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

Create a free website or blog at

%d bloggers like this: