Thursday, January 28, 2010

How to bind SharePoint event receivers to a custom list definition

In most cases you want to create your own list definition with your own event receiver that sould only react on events of your custom list.

SharePoint ships with alot of list definitions with unique list type numbers:

100: Generic list
101: Document library
104: Announcements list
and many more.

You can create event receivers that react on events of these list types.

To create a custom list definition you should use the visual studio template "List Definition" shipped with the VSeWSS 1.1. If you create a custom list with base type "document library" its id will also be 101. It's just a copy of the template you selected.

First line of schema.xml after creating the custom list definition project:

PLAIN TEXT

XML:

  1. <List xmlns:ows="Microsoft SharePoint"
  2.   Title="List Definition1"
  3.   Direction="$Resources:Direction;"
  4.   Url="MyDocumentLibrary"
  5.   BaseType="1"
  6.   Name="MyDocumentLibrary"
  7.   Id="cc1b7796-28cb-4eeb-9ca5-fdacfadddecc"
  8.   Type="101" 
  9.   xmlns="http://schemas.microsoft.com/sharepoint/">

So what you should do ist change the Type value to a non-used number:

PLAIN TEXT

XML:

  1. <List xmlns:ows="Microsoft SharePoint"
  2.   Title="List Definition1"
  3.   Direction="$Resources:Direction;"
  4.   Url="MyDocumentLibrary"
  5.   BaseType="1"
  6.   Name="MyDocumentLibrary"
  7.   Id="cc1b7796-28cb-4eeb-9ca5-fdacfadddecc"
  8.   Type="999" 
  9.   xmlns="http://schemas.microsoft.com/sharepoint/">

You also have to modify the ListDefinition.xml

PLAIN TEXT

XML:

  1. <Elements
    Id="cc1b7796-28cb-4eeb-9ca5-fdacfadddecc"
    xmlns="http://schemas.microsoft.com/sharepoint/">
  2.   <ListTemplate
    Name="BskhDocumentLibrary"

  3.                 DisplayName="My Dokument Library"
  4.                 Description="Custom document library with custom event receiver doing cool things."
  5.                 BaseType="1"
  6.                 Type="999"
  7.                 OnQuickLaunch="TRUE"
  8.                 SecurityBits="11"
  9.                 Sequence="110"
  10.                 Image="/_layouts/images/itdl.gif"
  11.                 DocumentTemplate="101"
    />
  12. </Elements>

and the ItemEvntReceiver.xml

PLAIN TEXT

XML:

  1. <Elements
    Id="faca01fe-8478-4116-8b72-1e243bda6268"
    xmlns="http://schemas.microsoft.com/sharepoint/">
  2.   <Receivers
    ListTemplateOwner="cc1b7796-28cb-4eeb-9ca5-fdacfadddecc"
    ListTemplateId="999">
  3.     <Receiver>
  4.       <Name>ItemDeleted</Name>
  5.       <Type>ItemDeleted</Type>
  6.       <SequenceNumber>1</SequenceNumber>
  7.       <Class>faca01fe-8478-4116-8b72-1e243bda6268</Class>     
  8.     </Receiver>
  9.     <Receiver>
  10.       <Name>ItemAdded</Name>
  11.       <Type>ItemAdded</Type>
  12.       <SequenceNumber>1</SequenceNumber>
  13.       <Class>faca01fe-8478-4116-8b72-1e243bda6268</Class>
  14.     </Receiver>
  15.     <Receiver>
  16.       <Name>ItemUpdated</Name>
  17.       <Type>ItemUpdated</Type>
  18.       <SequenceNumber>1</SequenceNumber>
  19.       <Class>faca01fe-8478-4116-8b72-1e243bda6268</Class>
  20.     </Receiver>
  21.   </Receivers>
  22. </Elements>

Now your event receiver will be called only by MyDocumentLibrary lists and no longer by all document libraries.

0 comments: