Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

What is the best practice around adding event receivers across all the lists in a Site Collection?

share|improve this question
    
see I have updated my response with the code. –  Falak Mahmood Jun 5 '12 at 14:34
    
If I'm not mistaken, creating a list item event receiver and scoping it to 'Site' instead of 'Web' will make the Receiver fire for all lists. –  Omar Stewey Sep 21 at 16:59

3 Answers 3

up vote 2 down vote accepted

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{




            }



    }

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; }
            }

        }
}
share|improve this answer
    
do you have code for Feature deactivation? –  Geek Jun 5 '12 at 14:49
1  
Yes, see the link: snahta.blogspot.se/2008/08/… –  Falak Mahmood Jun 5 '12 at 15:08
    
Do not dispose SPFeatureReceiverProperties.Feature.Parent Link: docs.spcaf.com/v5/… Do not dispose SPSite.RootWeb Link: docs.spcaf.com/v5/SPC110241_DoNotDisposeSPSiteRootWeb.html –  Sergey Sep 21 at 12:03

If you want an event receiver on anything, then it's best to attach it to the content type Item (0x01)

Example: CharlieDigital: Programmatically Adding an Event Receiver to a Content Type

share|improve this answer
1  
do you have any code available, Per? –  Geek Jun 4 '12 at 7:06
2  

In my opinion i cannot recommend to add the event receiver to the system content type because it will effect all content types below the system content type. This might lead to unwanted problems an conflicts when the event receiver is fired. What is the reason for adding a event receiver to every list in?

share|improve this answer
    
what alternative you have at the moment? –  Geek Jun 4 '12 at 7:05
    
One alternative is to create a feature event receiver that adds the event receiver to the required lists. I'm not quite sure why you want to add a event receiver to all lists. –  Stefan Bauer Jun 4 '12 at 11:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.