5

I attached an event handler for ItemAdded to a content type A in the xml (using XmlDocuments). I have 4 other content types A1, A2, A3 and A4 that inherit from content type A (in their xml definition there is no event receiver defined). I create a custom list and add A1, A2, A3 and A4 to it.

When I add a new item to it through the UI, my event receiver is called once (as expected). When I add an item programmatically (see code below), my event receiver is called 4 times. Each time it is called, I noticed that the SPItemEventProperties.Context.ReceiverId changes. I've had a look at the EventReceivers table in SQLServer and noticed that there are four entries for my event receiver, all identical except for the ID of the event receiver and the Source ID (each have one of the four list content type's IDs). I think this is normal.

To programmatically add the item to the list, I do

SPListItem item = list.AddItem();
item[SPBuiltInFieldId.ContentTypeId] = <my list content type ID>;
...setting other fields...
item.Update();

Any idea why it works as expected when adding an item through the UI but not when adding through code?

Thanks

2
  • Could you post the IDs of content type A, A1, A2, A3 and A4 and also the above code with the explicit content type id you are setting there? It might help to figure out the cause of the error.
    – Norbert
    Aug 23, 2013 at 6:55
  • I think the solution would be to attach my event receiver once on the list instead of attaching it to a base content type whose descendants are all added to the list, thus attaching it multiple times to the list. However, that still does not explain why my original solution works as expected when adding an item from the UI, that what I'd like to understand.
    – Alex
    Aug 23, 2013 at 12:39

4 Answers 4

2

I recently got exactly the same problem, had about 20 content types i list, so event receiver was running 20 times when adding an item programmatically, even if it was registered only once in List.EventReceivers collection.

It also runs only once when adding an item through UI.

The workaround of setting ContentType field before updating the item works perfectly for me. Found it here

So try to set ContentType field instead of ContentTypeId in your code.

I don't know the reason behind that behavior, for me it feels like some kind of bug/feature in SharePoint. Maybe when user adds an item through UI, the code actually set all the field values presented in the form, including ContentType, so it does exactly the same thing as in workaround.

8
  • So, are you actually saying, that the code Alex is presenting in his question, should work? Did it work in your case? Or what workaround are you talking about? Sorry, but it's not clear for me.
    – Norbert
    Aug 23, 2013 at 8:16
  • Sorry, just checked what I did exactly. Turns out to be ContentType field instead of ContentTypeId. Edited the answer and provided the link.
    – Tim
    Aug 23, 2013 at 8:24
  • I tried that same solution before with no luck. In the example they just do item["ContentType"] = item["ContentType"]. But how did you set it in the first place?
    – Alex
    Aug 23, 2013 at 12:28
  • Just noticed you mentionned in your case it was registered only once in the SPList.EventReceivers? In my case it appears there 4 times (and in the database I see it 4 times for my list - as HostID - but with 4 different SourceID). Still, when adding from the UI, it fires only once. I'd like to know what the UI is doing behind the scenes. Might have to go the reflector route.
    – Alex
    Aug 23, 2013 at 12:49
  • 1
    In my case I had ItemUpdated event receiver, not ItemAdded as in your case. So I had already Content Type set for the item. Then on the button click I was setting some other field values and then that row from example: item["ContentType"] = item["ContentType"] before calling item.SystemUpdate(false). In this case the event receiver was running only once, and without item["ContentType"] = item["ContentType"] row it was executed like 20 times.
    – Tim
    Aug 26, 2013 at 9:11
1

If you register the event receiver multiple times - it fires multiple time on update. Try this tool to see if the receiver is attached several times - http://speventreceiverman.codeplex.com/

1
  • But why does it only fire once when adding from the UI? In SP Manager I do see the event attached 4 times on my list (in the EventReceivers table I also see it 4 times on the HostID of my list but with 4 different SourceID - corresponding to my 4 list content types).
    – Alex
    Aug 23, 2013 at 12:33
1

Just to compliment Max's answer: I prefer using SharePoint Manager for checking Event Receivers.

enter image description here

You can even delete duplicate receivers from SPD.

1
  • I checked in SP Manager, it is registered multiple times for the list and there the SourceID does not seem to appear (whereas the SourceID appears in the EventReceiver database table to filter which event should occur from what source). However, why does it only fire once from the UI?
    – Alex
    Aug 23, 2013 at 12:30
1

Finally found it. In addition to the content type id, you also need to set the name of the content type:

listItem["ContentType"] = "MyContentTypeName";

I can't explain why it actually works though, I'd need to look at how the event receivers are resolved. I found the answer in the SaveButton webcontrol's source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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