As Per said, you can attach the following code to add the Event receivers to all the lists in a Site Collection:
All you need to do it to add this code in a Feature Receiver's class file. Note: I did it only for ItemAdding event but you can add other methods as per your likings.
public class Feature1EventReceiver : SPFeatureReceiver
{
// Uncomment the method below to handle the event raised after a feature has been activated.
private SPContentType fetchContentType(SPContentTypeCollection contentTypeCollection, string ID)
{
SPContentType publContentType = null;
foreach (SPContentType contentType in contentTypeCollection)
{
//if (contentType.Id.Equals(ID))
if (string.Equals(contentType.Id.ToString(), ID, StringComparison.InvariantCultureIgnoreCase))
{
publContentType = contentType;
break;
}
}
return publContentType;
}
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite siteCollection = properties.Feature.Parent as SPSite;
SPWeb myweb = siteCollection.RootWeb;
try
{
SPContentTypeCollection contentTypeCollection = siteCollection.RootWeb.ContentTypes;
SPContentType publPageContentType = fetchContentType(contentTypeCollection, "0x01");
publPageContentType.EventReceivers.Add(SPEventReceiverType.ItemAdding,
"Test.Test.Com, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ea51e61ab1378806",
"Test.Test.Com.EventReceivers");
publPageContentType.Update(true, false);
myweb.Update();
}
catch
{
return;
}
finally{
siteCollection.Dispose();
myweb.Dispose();
}
}
You EventReceivers.cs will look like
class EventReceivers : SPItemEventReceiver
{
public override void ItemAdding(SPItemEventProperties properties)
{
if (properties.Context != null)
{
try
{
//do something here
}
catch { return; }
}
}
}