Question

I am trying to create a feature that uses the API for associating an ItemAdded event receiver to a Discussion List. I was associating the Event Receiver to the SPList, however, i don't want the event to fire when Replies are created (Message Content Type), only when Threads (i.e. Discussion Content Types) are created.

In my situation, I have a Feature Receiver and essentially the following code:

SPWeb web = properties.Feature.Parent as SPWeb;
if (web.Lists["Team Discussion"].ContentTypes["Discussion"].EventReceivers == null)
  throw new Exception("null ...darn");

Will SharePoint allow me to associate event receivers on particular lists content types?

In this case, if the users add additional discussion boards, I don't want to register them with my event reciever, so I don't want to add the association at the web level.

EDIT: I actually need to handle ItemAdded, ItemUpdated, and ItemDeleted, not just ItemAdded. The CUD on the list is not very intensive, however, there are a few hundred instances of the list. I am aware of creating a new Content Type, however, this will require backporting hundreds of lists and migrating each OOB Discussion ListItem to a new Custom Discussion Content Type, not to mention will require testing to see if SPUtility.CreateNewDiscussion() will play nicely with the new custom Discussion CType.

Was it helpful?

Solution

Actually, it doesn't look like this is possible.

Using Reflector, here is the definition of the EventReceivers property for the SPContentType:

public SPEventReceiverDefinitionCollection EventReceivers
{
    get
    {
        return ((this.m_list != null) ? null : this.GetEventReceivers());
    }
}

So you can see, it's basically saying: "If the m_list is not null (i.e. we are looking at a List Content Type instance), then always return null".

OTHER TIPS

Would it not be better to just add some logic within your event receiver itself to ignore other content types and only take action on Discussion Content Types?

Updated

In response to your comment. Your question only mentions 'creation' which is why I suggested this approach. In terms of performance is this a CRUD heavy list? What sort of volume are we talking?

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top