Event Receivers on Content Types
Adding this to the category of things-that-I-didn’t-know-but-would-have-made-a-lot-of-stuff-I-previously-wrote-much-more-elegant-and-awesome.
You should add it to yours, too!
As a quick summary, it’s common knowledge (well, amongst SharePoint developers at least) that you can associate event receivers with a list template type. However, an interviewer recently brought to light that one can also associate an event receiver directly with a content type. This is immensely useful for anyone building custom solutions on SharePoint, especially if you make heavy usage of content types in your design.
Here’s an example using the same basic content type:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<font face="Lucida Console,monospace"><font color="#0000ff"><?xml </font><font color="#ff0000">version</font><font color="#0000ff">=</font><font color="#ff00ff">"1.0" </font><font color="#ff0000">encoding</font><font color="#0000ff">=</font><font color="#ff00ff">"utf-8" </font><font color="#800080">?</font><font color="#0000ff">></font> <font color="#0000ff"><</font><font color="#808000">Elements </font><font color="#ff0000">xmlns</font><font color="#0000ff">=</font><font color="#ff00ff">"http://schemas.microsoft.com/sharepoint/"</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">Field </font><font color="#800080">DisplayName</font><font color="#0000ff">=</font><font color="#ff00ff">"Model Code"</font> <font color="#800080">Name</font><font color="#0000ff">=</font><font color="#ff00ff">"Model_Code"</font> <font color="#800080">StaticName</font><font color="#0000ff">=</font><font color="#ff00ff">"Model_Code"</font> <font color="#800080">ID</font><font color="#0000ff">=</font><font color="#ff00ff">"{F0000000-0000-0000-0000-000000000001}"</font> <font color="#800080">Type</font><font color="#0000ff">=</font><font color="#ff00ff">"Integer"</font> <font color="#800080">SourceID</font><font color="#0000ff">=</font><font color="#ff00ff">"http://schemas.someusedcarinventory.com"</font> <font color="#800080">Group</font><font color="#0000ff">=</font><font color="#ff00ff">"My Custom Columns"</font><font color="#0000ff">/></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">Field </font><font color="#800080">DisplayName</font><font color="#0000ff">=</font><font color="#ff00ff">"VIN"</font> <font color="#800080">Name</font><font color="#0000ff">=</font><font color="#ff00ff">"VIN"</font> <font color="#800080">StaticName</font><font color="#0000ff">=</font><font color="#ff00ff">"VIN"</font> <font color="#800080">ID</font><font color="#0000ff">=</font><font color="#ff00ff">"{F0000000-0000-0000-0000-000000000002}"</font> <font color="#800080">Type</font><font color="#0000ff">=</font><font color="#ff00ff">"Text"</font> <font color="#800080">SourceID</font><font color="#0000ff">=</font><font color="#ff00ff">"http://schemas.someusedcarinventory.com"</font> <font color="#800080">Group</font><font color="#0000ff">=</font><font color="#ff00ff">"My Custom Columns"</font><font color="#0000ff">/></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">Field </font><font color="#800080">DisplayName</font><font color="#0000ff">=</font><font color="#ff00ff">"Make"</font> <font color="#800080">Name</font><font color="#0000ff">=</font><font color="#ff00ff">"Make"</font> <font color="#800080">StaticName</font><font color="#0000ff">=</font><font color="#ff00ff">"Make"</font> <font color="#800080">ID</font><font color="#0000ff">=</font><font color="#ff00ff">"{F0000000-0000-0000-0000-00000000003}"</font> <font color="#800080">Type</font><font color="#0000ff">=</font><font color="#ff00ff">"Text"</font> <font color="#800080">SourceID</font><font color="#0000ff">=</font><font color="#ff00ff">"http://schemas.someusedcarinventory.com"</font> <font color="#800080">Group</font><font color="#0000ff">=</font><font color="#ff00ff">"My Custom Columns"</font><font color="#0000ff">/></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">ContentType </font><font color="#800080">Name</font><font color="#0000ff">=</font><font color="#ff00ff">"Vehicle"</font> <font color="#800080">ID</font><font color="#0000ff">=</font><font color="#ff00ff">"0x0100FC000000000000000000000000000001"</font> <font color="#800080">Description</font><font color="#0000ff">=</font><font color="#ff00ff">"Used car inventory"</font> <font color="#800080">Group</font><font color="#0000ff">=</font><font color="#ff00ff">"My Custom Content Types" </font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">FieldRefs</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">FieldRef </font><font color="#800080">ID</font><font color="#0000ff">=</font><font color="#ff00ff">"{c042a256-787d-4a6f-8a8a-cf6ab767f12d}" </font><font color="#800080">Name</font><font color="#0000ff">=</font><font color="#ff00ff">"ContentType" </font><font color="#0000ff">/></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">FieldRef </font><font color="#800080">ID</font><font color="#0000ff">=</font><font color="#ff00ff">"{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" </font><font color="#800080">Name</font><font color="#0000ff">=</font><font color="#ff00ff">"Title" </font> <font color="#800080">Required</font><font color="#0000ff">=</font><font color="#ff00ff">"TRUE" </font><font color="#800080">ShowInNewForm</font><font color="#0000ff">=</font><font color="#ff00ff">"TRUE" </font><font color="#800080">ShowInEditForm</font><font color="#0000ff">=</font><font color="#ff00ff">"TRUE" </font><font color="#0000ff">/></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">FieldRef </font><font color="#800080">ID</font><font color="#0000ff">=</font><font color="#ff00ff">"{F0000000-0000-0000-0000-000000000001}" </font><font color="#800080">Name</font><font color="#0000ff">=</font><font color="#ff00ff">"Model_Code"</font><font color="#0000ff">/></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">FieldRef </font><font color="#800080">ID</font><font color="#0000ff">=</font><font color="#ff00ff">"{F0000000-0000-0000-0000-000000000002}" </font><font color="#800080">Name</font><font color="#0000ff">=</font><font color="#ff00ff">"VIN"</font><font color="#0000ff">/></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">FieldRef </font><font color="#800080">ID</font><font color="#0000ff">=</font><font color="#ff00ff">"{F0000000-0000-0000-0000-000000000003}" </font><font color="#800080">Name</font><font color="#0000ff">=</font><font color="#ff00ff">"Make"</font><font color="#0000ff">/></font> <font color="#000000"> </font><font color="#0000ff"></</font><font color="#808000">FieldRefs</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">XmlDocuments</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">XmlDocument </font><font color="#800080">NamespaceURI</font><font color="#0000ff">=</font><font color="#ff00ff">"http://schemas.microsoft.com/sharepoint/events"</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">spe:Receivers </font><font color="#800080">xmlns:spe</font><font color="#0000ff">=</font><font color="#ff00ff">"http://schemas.microsoft.com/sharepoint/events"</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">Receiver</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">Name</font><font color="#0000ff">></font><font color="#000000">VehicleCreatedHandler</font><font color="#0000ff"></</font><font color="#808000">Name</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">Type</font><font color="#0000ff">></font><font color="#000000">ItemCreated</font><font color="#0000ff"></</font><font color="#808000">Type</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">SequenceNumber</font><font color="#0000ff">></font><font color="#000000">1</font><font color="#0000ff"></</font><font color="#808000">SequenceNumber</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">Assembly</font><font color="#0000ff">></font><font color="#000000">My.Library, Version=1.0.0.0, Culture=neutral, </font> <font color="#000000"> PublicKeyToken=c5168cbbbb64acf7</font><font color="#0000ff"></</font><font color="#808000">Assembly</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">Class</font><font color="#0000ff">></font><font color="#000000">My.Library.EventReceivers.VehicleCreatedHandler</font><font color="#0000ff"></</font><font color="#808000">Class</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">Data </font><font color="#0000ff">/></font> <font color="#000000"> </font><font color="#0000ff"><</font><font color="#808000">Filter </font><font color="#0000ff">/></font> <font color="#000000"> </font><font color="#0000ff"></</font><font color="#808000">Receiver</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"></</font><font color="#808000">spe:Receivers</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"></</font><font color="#808000">XmlDocument</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"></</font><font color="#808000">XmlDocuments</font><font color="#0000ff">></font> <font color="#000000"> </font><font color="#0000ff"></</font><font color="#808000">ContentType</font><font color="#0000ff">></font><font color="#000000"> </font> <font color="#0000ff"></</font><font color="#808000">Elements</font><font color="#0000ff">></font></font> |
Oddly enough, I was trying to figure out what the Filter element does when I stumbled upon the stackoverflow posting by accident. As it stands, I still can’t figure out what the element is and what XML is valid for it. Perhaps some CAML query filter?