Freigeben über


Resolution: MOSS: Event handler being called twice on an imported list template

Behavior/Symptoms:

I registered an event handler with a (custom) content type and then added this content type to a (custom) list definition. I implemented the list definition as a feature and installed/activated it for a site collection. Then I created a list using this list definition and added some items to it. The content type and event handler worked as expected and I confirmed that there was only a single event handler registered with the list. Next I saved this list as a template (including its content) and created a new list using it, I found that the new list was registered for the same event handler twice with a different sequence number. One of the event handlers still uses the original sequence number while the sequence number of the other one was incremented by one.

Steps:

Senerio1:One Event Handler(EH)

  • Created a list

  • Attached an event handler using the below code to a specific list. only one EH attached.
    string assembly = "Custom_EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fcb03c2826a13954";
    string assemblyClass = "Custom_EventHandler.CustomEventHandler";

    using (SPSite site = new SPSite("<https://terminator:9003/>"))
    {
    using (SPWeb web = site.OpenWeb())
    {
    SPList list = web.Lists["MyCustomDocLib"];
    SPEventReceiverDefinition updating =
    list.EventReceivers.Add();
    updating.Class = assemblyClass;
    updating.Assembly = assembly;
    updating.SequenceNumber = 10001;
    updating.Type = SPEventReceiverType.ItemAdded;
    updating.Update();
    }
    }

  • Saved that list as a template and created a list based on the saved template,
    Only one EH is attached.

Senerio2: Two Event Handlers(EHs)

  • Created a feature to attach an EH to a list, list type 101
  • Created a list based on 101, only one EH attached
  • Saved that list as a template and created a list based on the saved template,
    There are two EHs attached.

Conclusion:
If the EH is attached at the web level (Senerio2), then whenever a list is created based on the template list which already has EH attached, the web level EH attachment will also come in picture while creating a list based on template hence two EHs will be available(one from template, another from web level).

“This is an expected behavior.”
Total no. of EHs of the list created based on a template = Template’s EHs + EHs registered for that list type.

Comments

  • Anonymous
    February 10, 2013
    so, what do you suggest, should we deactivate the feature at web level, then only one event receiver will be available to the list ??????

  • Anonymous
    December 01, 2014
    Hi, I had this issue , i wanted to move list from site collection which has event reciever web level to another one so i saved it as template then created on new sitecollection. so now even if i didnt activate the feature on site level , the EH will work and if i activated it , it will work twice how can i fix it?