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

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>