Answered by:
Attaching an event receiver to a custom list

Question
-
I know that I can create an event receiver and in the element file, specify the ListTemplateID of the type of list I want to assign it to, then I can deploy the feature to a web scope.
However, since all lists have specific Types like 100, 107 etc wouldn't this mean that if I created another list in this site of the same type (have the same ListTemplateID) that would mean that the event receiver would be attached to both lists now?
So if I wanted to create an event receiver that would just run with items in a single doc lib and yet I have multiple doc libs on this site, how can I single this one doc lib out among the rest since they all have the same ListTemplateID?
EMSaturday, October 17, 2009 5:21 PM
Answers
-
Hi,
>>See, I thought that this ListTemplateID had to be one of the standard types that were out of the box, like 100, 107 etc. But if not, that's great news!
If you assign the ListTemplateID to 100 or 104, then the event receiver gets attached to every list of that template Id. For example, if you choose 100, then your event receiver will be attached to every custom list in the site! And every list would be subscribed to your event receiver! This is why we use different template IDs for custom list and subsribe our event receivers only to that list.
Hope that helps.
Regards,
Chakkaradeep
SharePoint Developer - MCTS SharePoint Dev, WSS Dev
Intergen: http://www.intergen.co.nz
Twitter: http://twitter.com/chakkaradeep
Blog: http://www.chakkaradeep.com- Marked as answer by Etowah_man Sunday, October 25, 2009 5:39 PM
Sunday, October 18, 2009 9:22 PM
All replies
-
You can easily assign the event handler to a specific list either by using WSS API's or by using a free GUI tool as this one http://www.u2u.info/SharePoint/U2U%20Community%20Tools/EventHandlerExplorer.zip
Hope this helps- Proposed as answer by Haitham Mohammed Saturday, October 17, 2009 7:10 PM
Saturday, October 17, 2009 6:22 PM -
Excellent! I'll take a look at that tool.
However, the question was regarding the listtemplateID. Since you can't just make up your own template ID, then it appears that if you deploy an event receiver as a feature and it references a ListTemplateID and you have two such lists on the same site, both would get the event receiver.
Am I missing something here, is there a way when deploying the feature to make it more granular than just pointing to a ListTemplateID? I know I can programmatically do it, but I would like to be able to deploy this feature and have it setup in the feature.
Thanks!
EMSaturday, October 17, 2009 7:17 PM -
If you want to do this using a feature and don't want to attach the event receiver for all lists instances using the same ListTemplateID , you have two solutions either :
1- Creating your own list definition for your list with a unique Type ID and attach the event to this ID
and here is how to create your own list definition http://ari.provoke.co.nz/archive/2007/04/18/creating-a-custom-sharepoint-2007-list-definition.aspx
2- Saving your list as a template then changing the Template Type ID with a unique id rather than the reserved MOSS Template IDs and here is how http://www.phase2.com/blog/?p=89
Hope this helps- Proposed as answer by Haitham Mohammed Monday, October 19, 2009 11:00 AM
Saturday, October 17, 2009 9:07 PM -
Hi,
Using CAML :
I would suggest you to build a List Definition and List Instance so that you can specify the Template ID.
Below is an example of List Definition:
Note the Type attribute. This should be unique and identifies this custom list definition.
And below is an example of a List Instance, based on the above List Definition:
You can see that the TemplateType is associated with the Type ID in the List Definition.
Using Code:
Its very simple to add the event receivers for a List created using Object Model: eventReceiverClass);
string eventReceiverAssembly = "MyAssembly, Version=1.1.0.0, Culture=neutral, PublicKeyToken=5e4a77ec46c0cc45" string eventReceiverClass = "MyAssembly.EventReceiverClass" list.EventReceivers.Add(SPEventReceiverType.ItemAdded, eventReceiverAssembly, eventReceiverClass);
Hope that helps.
Regards,
Chakkaradeep
SharePoint Developer - MCTS SharePoint Dev, WSS Dev
Intergen: http://www.intergen.co.nz
Twitter: http://twitter.com/chakkaradeep
Blog: http://www.chakkaradeep.com- Proposed as answer by Chakkaradeep Chandran Monday, October 19, 2009 8:09 PM
Sunday, October 18, 2009 6:32 AM -
Thanks Chakkaradeep and Haitham,
Chakkaradeep, in regard to the Type, whenever I go into the element.xml file of my event receiver:
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers ListTemplateId="100">
<Receiver>
<Name>Job Opening Event Receiver</Name>
<Type>ItemAdded</Type>
<SequenceNumber>10000</SequenceNumber>
<Assembly>MyTestEventReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3631c91090ee3805</Assembly>
<Class>MyTestEventReceiver.TestItemReceiver</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
</Receivers>
</Elements>
Is the ListTemplateId going to be replaced by the Type '10001'?
Thanks!
EMSunday, October 18, 2009 10:45 AM -
>>Is the ListTemplateId going to be replaced by the Type '10001'?
If your List Template ID is 10001 , then Yes .
Regards,
Chakkaradeep
SharePoint Developer - MCTS SharePoint Dev, WSS Dev
Intergen: http://www.intergen.co.nz
Twitter: http://twitter.com/chakkaradeep
Blog: http://www.chakkaradeep.comSunday, October 18, 2009 11:35 AM -
Just to be clear, you said:
>>If your List Template ID is 10001, then Yes.
What I'm asking is, if I create my own list like you did above that had a Type of 10001, then in my event receiver elements.xml file, I would have:
<Receivers ListTemplateID="10001">
See, I thought that this ListTemplateID had to be one of the standard types that were out of the box, like 100, 107 etc. But if not, that's great news!
Thanks!
EMSunday, October 18, 2009 5:28 PM -
Hi,
>>See, I thought that this ListTemplateID had to be one of the standard types that were out of the box, like 100, 107 etc. But if not, that's great news!
If you assign the ListTemplateID to 100 or 104, then the event receiver gets attached to every list of that template Id. For example, if you choose 100, then your event receiver will be attached to every custom list in the site! And every list would be subscribed to your event receiver! This is why we use different template IDs for custom list and subsribe our event receivers only to that list.
Hope that helps.
Regards,
Chakkaradeep
SharePoint Developer - MCTS SharePoint Dev, WSS Dev
Intergen: http://www.intergen.co.nz
Twitter: http://twitter.com/chakkaradeep
Blog: http://www.chakkaradeep.com- Marked as answer by Etowah_man Sunday, October 25, 2009 5:39 PM
Sunday, October 18, 2009 9:22 PM -
You can bind the event receiver to a specific list using a Feature Receiver. You need to write a cleas which inherits SPFeatureReceiver and implement the FeatureActivated method. Here's example code for the FeatureActivated method which adds the event receiver to the "Rating History" list.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
web.Lists["Rating History"].EventReceivers.Add(
SPEventReceiverType.ItemAdded,
"Dhunter.SharePoint.Eventhandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0a26195ff03df1fd",
"Dhunter.SharePoint.Eventhandlers.RatingSummaryEventHandler");
}
With the event receiver you write the class to implement your business logic and deploy to the GAC. You don't need a feature xml for the event receiver as the binding is handled by the feature receiver.
Hope this helpsThursday, September 15, 2011 7:21 AM