Frage

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.

War es hilfreich?

Lösung

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);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit sharepoint.stackexchange
scroll top