<CharlieDigital/> Programming, Politics, and uhh…pineapples

26Aug/10Off

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:

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Field DisplayName="Model Code"
    Name="Model_Code"
    StaticName="Model_Code"
    ID="{F0000000-0000-0000-0000-000000000001}"
    Type="Integer"
    SourceID="http://schemas.someusedcarinventory.com"
    Group="My Custom Columns"/>
  <Field DisplayName="VIN"
    Name="VIN"
    StaticName="VIN"
    ID="{F0000000-0000-0000-0000-000000000002}"
    Type="Text"
    SourceID="http://schemas.someusedcarinventory.com"
    Group="My Custom Columns"/>
  <Field DisplayName="Make"
    Name="Make"
    StaticName="Make"
    ID="{F0000000-0000-0000-0000-00000000003}"
    Type="Text"
    SourceID="http://schemas.someusedcarinventory.com"
    Group="My Custom Columns"/>
  <ContentType Name="Vehicle"
    ID="0x0100FC000000000000000000000000000001"
    Description="Used car inventory"
    Group="My Custom Content Types" >
    <FieldRefs>
      <FieldRef ID="{c042a256-787d-4a6f-8a8a-cf6ab767f12d}" Name="ContentType" />
      <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" 
        Required="TRUE" ShowInNewForm="TRUE" ShowInEditForm="TRUE" />
      <FieldRef ID="{F0000000-0000-0000-0000-000000000001}" Name="Model_Code"/>
      <FieldRef ID="{F0000000-0000-0000-0000-000000000002}" Name="VIN"/>
      <FieldRef ID="{F0000000-0000-0000-0000-000000000003}" Name="Make"/>
    </FieldRefs>
    <XmlDocuments>
      <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/events">
        <spe:Receivers xmlns:spe="http://schemas.microsoft.com/sharepoint/events">
          <Receiver>
            <Name>VehicleCreatedHandler</Name>
            <Type>ItemCreated</Type>
            <SequenceNumber>1</SequenceNumber>
            <Assembly>My.Library, Version=1.0.0.0, Culture=neutral, 
                PublicKeyToken=c5168cbbbb64acf7</Assembly>
            <Class>My.Library.EventReceivers.VehicleCreatedHandler</Class>
            <Data />
            <Filter />
          </Receiver>
        </spe:Receivers>
      </XmlDocument>
    </XmlDocuments>
  </ContentType>  
</Elements>

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?

Posted by Charles Chen

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.