Tuesday, August 3, 2010

Attaching Event Receivers to Content Types

When you define a content type with a feature, you create 2 XML files as shown below:

  • Feature.xml – Use this XML file to define the metadata for the new feature. The ElementManifests element points to the location of the 2nd XML file storing all of the detailed information on the feature itself.
  • Elements.xml – Use this file to define the assembly that encapsulates the event handler, the class itself, and also a sequence number that specifies the order.

Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<ContentType ID="0x0100c07095261b4f48408d60ad8654b69d68" Name="DevelopingCT" Group="Development" Description="Developing Content Type" Version="0">
<FieldRefs>
<FieldRef ID="{0c177b88-bf35-4692-8456-cc14aaabecc0}" Name="DevelopingCTField" />
</FieldRefs>
<XmlDocuments>
<XmlDocument NamespaceURI="
http://schemas.microsoft.com/sharepoint/events">
<spe:Receivers xmlns:spe="
http://schemas.microsoft.com/sharepoint/events">
<Receiver>
<Name>ItemAdded</Name>
<Type>ItemAdded</Type>
<SequenceNumber>1</SequenceNumber>
<Assembly>AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=TokenNumber</Assembly>
<Class>Namespace.ClassName</Class>
<Data>
</Data>
<Filter>
</Filter>
</Receiver>
<Receiver>
<Name>ItemAdding</Name>
<Type>ItemAdding</Type>
<SequenceNumber>2</SequenceNumber>
<Assembly>AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=TokenNumber</Assembly>
<Class>Namespace.ClassName</Class>
<Data>
</Data>
<Filter>
</Filter>
</Receiver>
</spe:Receivers>
</XmlDocument>
</XmlDocuments>
</ContentType>
</Elements>


Note: Event receivers in content types are defined in a different section of the elements file. The Receiver XmlDocument resides in the http://schemas.microsoft.com/sharepoint/events namespace.

Related post: Writing Event Receiver Class in Code

References:

http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/fecdfc46-3223-4b9d-b218-29bf475b9cd6

http://stackoverflow.com/questions/561835/how-do-you-bound-an-event-receiver-to-a-specific-custom-content-type

http://www.wrox.com/WileyCDA/Section/Programming-Event-Handling-in-Windows-SharePoint-Services.id-306329.html

http://download.microsoft.com/download/b/d/4/bd45c0a8-74bc-4e3a-9735-a715b902f89c/03-EventHandlers.docx

No comments:

Post a Comment