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:
- <List xmlns:ows="Microsoft SharePoint"
- Title="List Definition1"
- Direction="$Resources:Direction;"
- Url="MyDocumentLibrary"
- BaseType="1"
- Name="MyDocumentLibrary"
- Id="cc1b7796-28cb-4eeb-9ca5-fdacfadddecc"
- Type="101"
- xmlns="http://schemas.microsoft.com/sharepoint/">
So what you should do ist change the Type value to a non-used number:
- <List xmlns:ows="Microsoft SharePoint"
- Title="List Definition1"
- Direction="$Resources:Direction;"
- Url="MyDocumentLibrary"
- BaseType="1"
- Name="MyDocumentLibrary"
- Id="cc1b7796-28cb-4eeb-9ca5-fdacfadddecc"
- Type="999"
- xmlns="http://schemas.microsoft.com/sharepoint/">
You also have to modify the ListDefinition.xml
- <Elements Id="cc1b7796-28cb-4eeb-9ca5-fdacfadddecc" xmlns="http://schemas.microsoft.com/sharepoint/">
- <ListTemplate Name="BskhDocumentLibrary"
- DisplayName="My Dokument Library"
- Description="Custom document library with custom event receiver doing cool things."
- BaseType="1"
- Type="999"
- OnQuickLaunch="TRUE"
- SecurityBits="11"
- Sequence="110"
- Image="/_layouts/images/itdl.gif"
- DocumentTemplate="101" />
- </Elements>
and the ItemEvntReceiver.xml
- <Elements Id="faca01fe-8478-4116-8b72-1e243bda6268" xmlns="http://schemas.microsoft.com/sharepoint/">
- <Receivers ListTemplateOwner="cc1b7796-28cb-4eeb-9ca5-fdacfadddecc" ListTemplateId="999">
- <Receiver>
- <Name>ItemDeleted</Name>
- <Type>ItemDeleted</Type>
- <SequenceNumber>1</SequenceNumber>
- <Class>faca01fe-8478-4116-8b72-1e243bda6268</Class>
- </Receiver>
- <Receiver>
- <Name>ItemAdded</Name>
- <Type>ItemAdded</Type>
- <SequenceNumber>1</SequenceNumber>
- <Class>faca01fe-8478-4116-8b72-1e243bda6268</Class>
- </Receiver>
- <Receiver>
- <Name>ItemUpdated</Name>
- <Type>ItemUpdated</Type>
- <SequenceNumber>1</SequenceNumber>
- <Class>faca01fe-8478-4116-8b72-1e243bda6268</Class>
- </Receiver>
- </Receivers>
- </Elements>
Now your event receiver will be called only by MyDocumentLibrary lists and no longer by all document libraries.

Hi, Blogger, nice to meet you. I wish to ask one question here. Thank you.
I tried Sharepoint EventHandler ItemAdding, having registered via codes and testified its correct functionality.
But when I changed to use feature.xml and stsadm command to register it, I met problem. The first time I successfully registered, but the next time I`m changing it and re-registered it, it didn`t change in .gif file and functionality as well. I guess something wrong in my way of deployment, This is my steps:
1. iisreset
2. stsadm.exe -o deactivatefeature -filename ../feature.xml -url .. -force
3. stsadm.exe -o uninstallfeature -filename ../feature.xml -force
4. iisreset
5. stsadm.exe -o installfeature -filename ../feature.xml -force
6. stsadm.exe -o activatefeature -filename ../feature.xml -url .. -force
My Question:
Where should we install feature to? if I want it to function on http://server/Test3/Nick5,
should I refer to this url in the following commandings?
BIN> stsadm.exe -o installfeature -filename “NickTest10\feature.xml” -url http://Server/Test3/Nick5” target=”_new” rel=”nofollow”>http://Server/Test3/Nick5
Or I`d better to register it firstly into “Site Collection Features” writing like the following way:
BIN> stsadm.exe -o installfeature -filename “NickTest10\feature.xml” -url http://Server
or, Should I register it as a solution by sdk command?
Thank you very much!
Nick.
Comment on July 8, 2008 @ 05:22:50
Hi Nick,
the receivers element can only be used in features scoped at the Sitelevel (Scope=”web”)
I would suggest to deploy the feature as Solution via stsadm…
Greetings
Michael
Comment on July 9, 2008 @ 11:29:37
Hi Nick,
as Michael said, the best practice to deploy a feature is a sharepoint solution.
The SharePoint Solution can be deployed to Site Collections. In addition solution deployment can be managed from the central administration.
If you install a feature not boxed into a solution it will be deployed globally on the farm. I think you cannot install a feature into a single site collection.
Maybe this helps,
Alexander
Comment on July 9, 2008 @ 11:50:22
Hi Michael and Alexander,
Thank you so much! Now I`m on my way to “Solution + feature”, and its deployment depends on .wsp or.wsp.cab file. I almost reach the final step, except for one problem.
I use tool to help feature deployment, which is STSDEV(has WPSBuilder quick-start function embodied). It builds automatically a good files structure in the solution folder, and has a series of function like “WSPBuilder, Deploy, to 12 hive, etc.”
My problems lays on here:
1. Can instsallsolution, but cannot deploysolution whenever I add -url parameter.
2. For hundreds of time, Given that I use feature deployment way, the eventHandler never inspire, never work nor be able to debug steply.
Why my .dll works and debug well when pragramatically deploy and so pecimistically lousy by the feature approach?
Wish to get your kind instructions, thank you again!!
Bin> stsadm -o addsolution -filename “D:\NickTest160\NickTest.wsp” (Successful!)
stsadm -o execadmsvcjobs
stsadm -o deploysolution -name NickTest.wsp -url http://localhost -immediate -allowgacdeployment -force (On This step I Fail! SDK Command Line following gave me such notice:
“This solution contains no resources scoped for a web application and cannot be deployed to a particular web application.”
I tried again, by removing the -url parameter, and succeed to pass, why? Should one feature be deployed on some level of web, or list?
mnn, run it, and EventHandler still doesn`t inspire.. My heart sink.
My Feature ID, Solution ID, all .xml files has been carefully overlooked, seems good enough. But Why Lack of Resources? is it that Something more must be embodied in .wsp.cab compact file? I uncompressed it and see, there are:
NickTest180.dll
manifest.xml
(Folder)OssurEventHandler {feature.xml; elements.xml}
I looked into relative materials on web, but failed to find the solution. Really hope you help me. Thank you!!
Best Regards.
Nick.
Comment on July 11, 2008 @ 02:49:23
Hi Nick,
what’s the scope of your feature? In your feature.xml the scope should be set to “Web”. It seems you have set it to “Site”.
Have you considered to use the Microsoft Visual Studio Extensions for SharePoint?
They provide a project type “EventReceiver”. It also creates the deployment files you need (wsp and bat).
http://www.microsoft.com/downloads/details.aspx?familyid=3E1DCCCD-1CCA-433A-BB4D-97B96BF7AB63&displaylang=en
To check if your EventHandler is correctly attached to your list you can use this useful tool:
http://www.entwicklungsgedanken.de/2008/02/29/tool-eventreceiver-installer-for-sharepoint/
I hope this helps,
Alex
Comment on July 11, 2008 @ 09:19:12
Hi Alexander,
You are so kind and helpful, so much indebted to your time.
1. My scope of feature in feature.xml is set “Web” definitely.
2. Microsoft web extension also gave good structure, I tried it at the early stage of my trial and test, but then I didn`t aware feature be boxed in as .wsp solution.
Now, I`ll try it again following the light of the above two links, with so much gladness and gratitude to you.
Best regards and nice weekend!
Nick.
Comment on July 11, 2008 @ 10:40:56
Hi Nick,
to create an Event Receiver with the Visual Studio Extensions, you have to create an “Empty” SharePoint project and add a new item called “Event Receiver”.
Regards,
Alex
Comment on July 11, 2008 @ 10:55:37
Hi Alexander,
Good afternoon, Thank you so much for your last instructions that are detailed and warming.
I use Eric`s tool “EventReceiverTool V1.1″ that you introdued to me, and realize all the effects. I`m so glad, but also awaring it`s only the one way, say, registering by code, not by feature.xml. This tool is not designed to deploy feature and box .wsp solution, so pity. but it`s really great to use.
For the purpose of learning, I wish to try feature & solution approach to deploy my EventHandler program. Now I`m confused at three probelms:
1. the last SDK command line, it denies on DeploySolution to a certain website(stsadm -o deploysolution -name “..” -url “…”).
2. There maybe some tool to generate .wsp file, but on which level of file foler should I put the files generated like .wsp file or even deployment folder?
3. What`s the cure for my pain? my program runs well with code deployment, while never triggered nor functioned when deployed by feature & solution and SDK stsadm?
It`s a process to make me torture, but also a chance to make me know kind and capable person like you Alex. I`m very glad.
Best regards.
Nick.
Comment on July 14, 2008 @ 10:45:14
Hi Alexander,
I tried SharePoint workflow + infopath 2007 this morning, and the deployment is quite easy, I finished everything quite soon.
But take a glimp to my EventHandler which remain unresolved, I`m almost faint about it.. is it that EventHandler is really not properly deployed as a feature? or maybe it`s hard to do because it`s not like workflow or webpart that has their special SDK? Waiting for some true knowledge from you, and meanwhile, keep trying every possibility.
My best regards.
Nick.
2008-7-15
Comment on July 15, 2008 @ 08:28:57
Hi Alexander,
A short while ahead I made it to deploy EventHandler as feature to any designated sharepoint page. mnn, still cannot summarize where I was blocked.
Little things can make a world to make me dump in, and when I wake up, it feels so much magic that doesn`t contribute a word.
I`ll record the process again, and a little later shall carefully publish here.
Thank you for your most valuable company, I`ll always visit your blog and learn here.
Best Wishes.
Nick.
Comment on July 15, 2008 @ 10:10:53
Hi Nick,
thank you for your kind words. We really appreciate it. And I am happy that you finally found the solution to your problem.
Cheers,
Tobi
Comment on July 15, 2008 @ 10:52:15
Hi Nick,
I’m glad you made it. How did you fix your problem?
Best regards,
Alex
Comment on July 15, 2008 @ 10:53:23
Dear Alexander and Tobias,
Thanks you so much! I`ve really got the right result by using Feature deployment way. Now it can be step debugged, and functions what I want. Happy that I can move to next work.
To look back, I`ve wandered many ways to test this simple function, really. now but other ways like deploy feature as .wsp still remain unconquered by me.
1. Deploy EventHandler via code.
1.1 console + class library.
1.2 use “EventHandlerExplorer”. it can be searched on-line by its name. Indeed a good Tool.
1.3 use “EventReceiver-installer-v1.1″, which Sir. Alex introduced to me. Can register and unregister eventhandler on various level of site`s object. So good for quick scanning.
2. Deploy EventHandler via feature.xml
2.1 console + class library + feature.xml and elements.xml in 12 hive.
2.2 use “WSPBuilder”. It`s a tool that can generate .wsp and .ddf and manifest.xml and .cab files automatically.
However, The problem is that I didn`t know how to make their position suitable in file structure, or if I must new a folder to hold them? At last I got no result and gave up.
2.3 my so-called best practice:
(1) Dilemma: .wsp or feature? say,use addsolution, or installfeature?
I tried first, failed to function, but the latter, pass successfully. And more, result is okay by designating onto any certain web, certain List! It`s ok.
(2) It`s hard while you construct file structure and fill in .xml files all by yourself. I highly recommend that we use tool, then this task is a piece of cake.
Task deployment time is estimated as within three minutes.
(3) somewhere they can use code snippet management to build feature.xml and elements.xml. it`s good for workflow, but here, I use such a famous tool: “STSDEV”.
Sir. Alex ever kindly suggest me to use VS sharepoint extension. But I`m a little poor in analyzing when I meet a problem that: VS extension cannot automatically generate a file folder that has healthy feature.xml and elements.xml infomation. I got easy to make error and keep unaware. while STSDEV gave me all good file structure and .xml content in the solution. So STSDEV is really a fast tool for someone new to EventHandler. (Web Part and workflow take another way)
My step is finally summarized as:
1. STSDEV to new an “Empty Solution”
2. Add assembly -> choose WSPBuilder, not SharePoint ->Event Handler.
3. Add existing key.snk that generated from the beginning STSDEV wizard.
4. Add Code, Build Project, Deploy .dll into GAC by gacutil.exe or dragging it to assembly foler.
5. check feature.xml(a little syntax error you`ll see) and elements.xml by adding .dll`s publictoken from GAC.
so, feature, elements, dll. these three all have their GUID and can easily be connected.
6. Mind that, everytime you build your project, use rightButtonClick to copy solution part to 12 hive. and iisreset.
7. USE FEATURE TO INSTALL AND ACTIVATE ON any WEB or LIST you want:
Finally it`s okay, I`m so glad to pass it with your notice and care.
Thousands of thanks.
Best wishes and good night.
Nick.
2008-7-15.
Comment on July 15, 2008 @ 14:25:52
sorry, the last sentence induced from the above comment are missing.
1.
2.
3.
Comment on July 15, 2008 @ 14:32:58
I haven`t found my problem, but I know, although I was careful and learning, they both are not enough. I`ll try and keep learning from you. It`s my honor to be here.
Thank you.
Nick.
Comment on July 15, 2008 @ 14:34:55
1. stsad.exe -o installfeature -filename namedprivyfeature\feature.xml
2. stsad.exe -o activatefeature -filename namedprivyfeature\feature.xml -url http://localhost/myweb/mylist -force
3. stsad.exe -o activatefeature -filename namedprivyfeature\feature.xml -url http://localhost/myAnotherweb/myAnotherlist -force
Comment on July 15, 2008 @ 14:36:45
1. stsadm -o installfeature -filename namedprivyfeature\feature.xml
2. stsad -o activatefeature -filename namedprivyfeature\feature.xml -url http://localhost/myweb/mylist -force
3. stsad -o activatefeature -filename namedprivyfeature\feature.xml -url http://localhost/myweb/mylist -force
Comment on July 15, 2008 @ 14:37:54
Hi Nick,
thank you for your very detailed step-by-step instructions. It’s a good alternative to the SharePoint Extensions for Visual Studio. In some cases you want or have to create the deployment files on your own (eg. Timer Jobs).
But I still recommend using the Extensions for EventReceiver because they deliver a SharePoint project template and a project item type called “EventReceiver”.
The project template does all the deployment for you and creates a .wsp and .bat file. The EventReceiver item template allows you to select a list type you want to attach to and creates the xml file to configure your event receiver.
Maybe I’ll post a step-by-step article to do so during the next days. Then we have both possible ways documented on this blog.
Best regards,
Alex
Comment on July 15, 2008 @ 15:06:24
Hi there,
I have a list which contains both files and folders but I only want the feature to be available for files and NOT folr folders – is there any way to achieve this?
Many thanks,
- Seamus
Comment on July 24, 2008 @ 16:56:15
Hi there,
Please post the complete source code for “How to bind SharePoint event receivers to a custom list definition”.
waiting for your reply.
Thanks in advance,
Shane
Comment on May 20, 2009 @ 06:59:34