kamagra how much to take

Targeting Event Receivers To Content Types

I was recently trying to target an event receiver that overrode the ItemUpdated event to a specific base content type, which, as I found out, is not easy to do, in the realm that building an event receiver that deploys via a SharePoint feature, you use the templateID and optionally use the ListTemplateOwner attribute.

  1. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  2. <Receivers ListTemplateId="101" ListTemplateOwner="54a516b6-2da8-4d2e-bd32-ca67cf093aca">
  3. <Receiver>
  4. <Name>receiverName</Name>
  5. <Type>ItemUpdated</Type>
  6. <Assembly>
  7. name, Version=1.0.0.0,  Culture=neutral, PublicKeyToken=*******************
  8. </Assembly>
  9. <Class>receiverClassName</Class>
  10. <Data />
  11. <Filter />
  12. </Receiver>
  13. </Receivers>
  14. </Elements>

Well, this isn’t very good, because I want to target a base content type, not a list template, or pass in a list ID to target even, that is entirely too limiting IMHO. I would have thought that there would have been a ContentType attribute for targeting with the Receivers element (I mean, come on, the freakin SPContentType object has an EventReceivers collection, as you will see in the below code), however I guess I was wrong, which is stupid IMHO by MSFT when designing receiver deployments since receivers are such much easier to write than the throwback event sinks that were offered in V2 which makes them attractive for trapping behavior. To get around this problem, I just use a feature receiver to associate my event receiver with a content type, when the event receiver is deployed. So, the receiver is just associated with the content type on Feature activation. I really find this solution frustrating, however it works fine, and it gets the job done.

  1. using Microsoft.SharePoint;
  2. using System.Reflection;
  3.  
  4. namespace exampleContentTypeActivation
  5. {
  6. public class exampleFeatureReceiver : SPFeatureReceiver
  7. {
  8.  
  9. public override void FeatureInstalled(SPFeatureReceiverProperties properties) { }
  10. public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { }
  11. public override void FeatureDeactivating(SPFeatureReceiverProperties properties){ }
  12. public override void FeatureActivated(SPFeatureReceiverProperties properties)
  13. {
  14. using (SPSite siteCollection = (SPSite) properties.Feature.Parent)
  15. {
  16. using (SPWeb site = siteCollection.OpenWeb())
  17. {
  18. SPContentType contentType = site.AvailableContentTypes["Document"];
  19. Assembly assembly = Assembly.GetExecutingAssembly();
  20. SPEventReceiverDefinition reciever = contentType.EventReceivers.Add();
  21. reciever.Name = "example_Name";
  22. reciever.Type = SPEventReceiverType.ItemUpdated;
  23. reciever.SequenceNumber = 10000;
  24. reciever.Assembly = string.Format("{0},{1},{2},{3}", assembly.GetName().FullName, assembly.GetName().Version, assembly.GetName().CultureInfo, assembly.GetName().GetPublicKey());
  25. reciever.Class = "example_Class_Name";
  26. reciever.Update();
  27. }
  28. }
  29. }
  30. }
  31. }

Ok, so in the above code, I am trying to keep this as dynamic as possible by reading the various assembly attributes and constructing them within the Feature receiver code, since I am compiling both the event receiver class and the Feature receiver class within the same assembly. Therefore, we can just use System.Reflection in order to get a lot of the attributes that are required in order to construct the receiver code, since we just need things like the culture info through the CultureInfo property and the Public Key token using the GetPublicKey() token method, etc. etc. etc.
That being said, I find not having this functionality where you can target a specific content directly in an element, without using such a bullshit workaround very, very silly. I think that there should be within the Receivers element an option to target a content type with its own attribute, as both of these pieces of technology were something that I was very excited about with the V3. Not making them marry when I want them to is straight up stupid.

Share

6 Comments

  1. Vijay says:

    I need to write an event for mouse hover or onclick on document library to display some message.

    Any advise?

  2. Joe Breen says:

    Hi Adam,

    I agree with you on this … they made something that should be simple really really difficult.

    I’ve tried your methods above in a VS 2005 project but it doesn’t associate the Feature only with the Content Type. It associates with a ListTemplateId also. In the MSFT documentation it says that ListTemplateId is OPTIONAL but in fact if you try to activate without the ListTemplateId in the node then it will error out.

    I don’t have a solution for this yet. I really need to activate only for a content type.

    Any ideas?

  3. Artur says:

    Let me say the code above is not working… in order to make it right you need to set Assembly properly meaning
    “AssemblyFullName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXX” – you miss keywords Version, Culture etc.

  4. KevinHou says:

    I spent several hours trying to get this code to work. Bottom line is you’re code is incorrect in two places.
    The collection returned from “site.AvailableContentTypes” is readonly, use “site.ContentTypes”
    You must include contentType(true) at the end. The “true” is a must for this to work on List content types (which I have)
    KevinHou

  5. krishna says:

    hi,

    How do i use the above solution, i cant make it work for a web, how should i create the object for this receiver class to invoke activation.

    thanks

  6. krishna says:

    hi,

    In my scenario web.ContentTypes returns count as zero, can you suggest if there is a flag i need to enable?

    Thanks

Trackbacks/Pingbacks

  1. Sharepoint Event Receivers Target Content Type « Sladescross's Blog - [...] Sharepoint Event Receivers Target Content Type http://www.sharepointsecurity.com/general/targeting-event-receivers-to-content-types/ [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>