3

I need to develop and deploy an event receiver (A) to a specific document library <1> and another event receiver (B) to another specific email-enabled document library <2>; both document libraries reside in the same SharePoint website (see image which is sketchy at best!!). enter image description here

This is a MOSS 2007 environment, by the way, and I am using Visual Studio 2008 with WSPBuilder. I would like to do the following and I would like to know if it is feasible

  • Set up a single Visual Studio solution that will contain the implementation for the two event receivers.
  • A single feature must be installed and used to simultaneously enable (or disable) both event receivers.
  • Upon activating the feature, bind event receiver <1> to document library (A), and bind event receiver <2> to document library (B). Conversely, when deactivating the feature, unbind the event receivers from the corresponding document libraries.

What is the best approach to implementing the above, and am I deviating from best practices with this type of packaging? My goal is not about convenience; it is necessary for both event receivers to be functional. If the event receivers were implemented as separate features, it is possible (user error) that one feature is activated while the other is not.

7

Your plan is good, and respects good practices (at least the practices I've been following for ages).
Your approach is to:

  1. Create the solution in VS
  2. Create one feature (scope: Web)
  3. Create two classes for the event receivers (both inheriting from SPItemEventReceiver)
  4. Add a feature event receiver on the feature (inheriting from SPFeatureReceiver)
  5. In the code of the feature event event receiver, add code in FeatureActivated and in FeatureDeactivating to support programmatic registration (unregistration) of the list ER (see code below). You'd first need to get a reference to the lists you want to target, and then register the ER on them.
  6. Wire all it up with WSPBuilder

Code to register a list ER on a given list:

private static void EnsureER<T>(SPList list, string name, SPEventReceiverType type)
    where T : SPItemEventReceiver
{
    // We get the assembly/class names from the T generic type
    string assemblyName = typeof(T).Assembly.FullName;
    string className = typeof(T).FullName;

    // We first remove any existing def matching the same criterias
    List<SPEventReceiverDefinition> defToRemove = new List<SPEventReceiverDefinition>();

    SPEventReceiverDefinitionCollection erCollection = list.EventReceivers;

    foreach (SPEventReceiverDefinition def in erCollection)
    {
        if ((def.Class == className) && (def.Assembly == assemblyName) && (def.Type == type))
            defToRemove.Add(def);
    }
    foreach (SPEventReceiverDefinition def in defToRemove)
        def.Delete();

    SPEventReceiverDefinition newDef = erCollection.Add();

    newDef.Assembly = assemblyName;
    newDef.Class = className;
    newDef.Name = name;
    newDef.Type = type;
    newDef.SequenceNumber = 1000;
    newDef.Update();
}

Usage:

Utils.EnsureER<MyListERClass>(myList, "MyER_ItemAdded", SPEventReceiverType.ItemAdded);
  • Thank you for the detailed answer and a very good looking ER binding utility method! The noob that I am, I need a little more hand-holding, as apparent from the following questions. (a) What type of feature should I select in WSPBuilder, for step# 2? (b) Should I select the Feature with Receiver item in WSPBuilder for step# 4? (c) Wiring up it all in WSPBuilder is where I lack the experience rather acutely, since I limited myself to using its wizard like interface. I'm wondering, based on how you would do something like this, do you end up with a single feature.xml and elements.xml? – Web User Dec 15 '15 at 7:59
  • 1
    Adding an item "Feature With Receiver" in WSPBuilder will cover both steps #2 and #4. Creating the list event receivers' classes (step #3) will be done manually by adding standard classes to the project. Step #5 is adding code manually in the feature evnet receiver (based on the sample I provided). Last step is creating the package by building the WSP from WSPBuilder. – Evariste Dec 15 '15 at 10:07
  • that worked out really well. Not only did you answer my question, but you gave me some neat, reusable code, as well as helping me understand WSPBuilder a little better. Thank you!!! – Web User Dec 15 '15 at 13:15
  • I am having trouble with the event receiver and since it is related to this question from a follow-up perspective, I thought I will bring a new Stackexchange question to your attention. – Web User Dec 16 '15 at 13:59
  • How can I access a web.config configuration key's value in the FeatureEventReceiver class? – Web User Jul 10 '17 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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