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