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

I have an event receiver on item deleting which is being activated through code, the following is my code:

SPContentType myContentType = web.ContentTypes["abc"];

//class where event receiver methods are

        Type eventReveiverType=typeof(PageEventReceiver);

        SPEventReceiverDefinition def = myContentType .EventReceivers.Add();
        def.Assembly = eventReveiverType.Assembly.FullName;
        def.Class = eventReveiverType.FullName;
        def.Name = "ItemDeleting";
        def.Type = SPEventReceiverType.ItemDeleting;
        def.SequenceNumber = 1000;
        def.Update();

        myContentType .Update(true);

The problem is that this event receiver is firing on all content types not just the one specified above. can anyone help pls

share|improve this question
    
Is "abc" item by any chance? –  SpartanDonut Mar 1 '12 at 16:39
    
abc is a content type –  IanCian Mar 2 '12 at 7:57
    
Item is a content type. The reason I ask is because if abc is Item then of course its going to affect all other content types because all content types inherit from Item. –  SpartanDonut Mar 2 '12 at 13:59

1 Answer 1

In your FeatureActivated handler you need to iterate through your lists and pick the the list with the Content Type "abc" for the item event handler.

See this 2-part series for details - Event Receivers Part 1- Create Event Receiver for Content Types. (part 2 has the FeatureActivated code).

[UPDATE] To elaborate the details (adapted from part 2 above) -

1). register for all lists using the content type - this would be in FeatureX.EventReceiver.cs

 public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWeb web = properties.Feature.Parent as SPWeb;
                ReplaceParameters(properties.Feature.Properties);

                SPListCollection lstCollection = web.Lists;

                foreach (SPList lstToUpdate in lstCollection)
                {
                    if (lstToUpdate.ContentTypes["fooContentType"] != null)
                    {
                        SPEventReceiverDefinitionCollection defColl = lstToUpdate.EventReceivers;
            // do ItemDeletion class & def registration here
    ... catch, etc..
        }

    }

2). In the list item deletion event handler - this would be in the ItemDeletionEventReceiver.cs

      public override void ItemDeletion(SPItemEventProperties properties)
        {
            base.ItemDeletion(properties);   

            try
            {
                properties.Web.AllowUnsafeUpdates = true;
                this.EventFiringEnabled = false;

        itemList = properties.List;

        // check for ContentType of list
        // if contentype = "fooContentType"  //"abc"
        //  ITEMDELETION action, etc....
    ... catch, etc..
    }

}

[UPDATE2]:

Here's another example & approach - Developing an Event Receiver for a Content Type

Step 4 in this example uses ItemAdded but can be replaced by another event. Also, note that the event receivers are custom built (not by Wizard) for Content Type. The bottom part of Step 4 shows how to link the event receiver to the Content Type ("abc") and Step 5 shows how to modify the Content Type to add the Event Receiver.

share|improve this answer
    
This only holds true if he only wants it to run on one list instead of all lists that use that content type. To me it sounds like he wants it to run on all lists that use the content type but its actually running for all content types on all lists. Very good reference though! –  SpartanDonut Mar 1 '12 at 20:24
    
thanks. I have elaborated with some code (adapted from part 2 above). –  Supriyo SB Chatterjee Mar 1 '12 at 21:35
1  
This event receiver is attached to a content type 'abc' not a list. this content type is being set in a publishing web pages library and whenever a delete is called on the list (pages library) it is being executed no matter what content type the page has. –  IanCian Mar 2 '12 at 8:03
    
@IanCian - I have updated my above answer with another example & approach. –  Supriyo SB Chatterjee Mar 2 '12 at 15:09

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.