sharepointeventreceiver

Error registering SharePoint WebDeleting event receiver in some environments


I am trying to register a WebDeleting event receiver within SharePoint. This works fine in my development environment, but not in several staging environments. The error I get back is "Value does not fall within the expected range.". Here is the code I use:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite elevatedSite = new SPSite(web.Site.ID))
    {
        using (SPWeb elevatedWeb = elevatedSite.OpenWeb(web.ID))
        {
            try
            {
                elevatedWeb.AllowUnsafeUpdates = true;
                SPEventReceiverDefinition eventReceiver = elevatedWeb.EventReceivers.Add(new Guid(MyEventReciverId));
                eventReceiver.Type = SPEventReceiverType.WebDeleting;
                Type eventReceiverType = typeof(MyEventHandler);
                eventReceiver.Assembly = eventReceiverType.Assembly.FullName;
                eventReceiver.Class = eventReceiverType.FullName;
                eventReceiver.Update();
                elevatedWeb.AllowUnsafeUpdates = false;
            }
            catch (Exception ex)
            {
                // Do stuff...
            }
        }
    }
});

I realize that I can do this through a feature element file (I am trying that approach now), but would prefer to use the above approach.

The errors I consistently get in the ULS logs are:

03/11/2010 17:16:57.34  w3wp.exe (0x09FC)                           0x0A88  Windows SharePoint Services     Database                        6f8g    Unexpected  Unexpected query execution failure, error code 3621. Additional error information from SQL Server is included below. "The statement has been terminated." Query text (if available): "{?=call proc_InsertEventReceiver(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}"  
03/11/2010 17:16:57.34  w3wp.exe (0x09FC)                           0x0A88  Windows SharePoint Services     General                         8e2s    Medium      Unknown SPRequest error occurred. More information: 0x80070057   

Any ideas?


UPDATE - Some interesting things I have learned...

I modified my code to do the following:

I deployed this change along with some extra trace statements that go to the ULS logs and after doing enough iisresets and clearing the GAC of my assembly, I started to see my new trace statements in the ULS logs and I no longer got the error!

So, I started to go back towards my original code to see what change exactly helped. I finally ended up with the original version in source control and it still worked :-S.

So the answer is clearly that it is some caching issue. However, when I was originally trying to get it to work I tried IISRESETs, restarting some SharePoint services OWSTimer (this, I believe runs the event hander, but probably isn't involved in the event registration where I am getting the error), and even a reboot to make sure no assembly caching was going on - and that did not help before.

The only thing I have to go on is maybe following steps such as:

  1. Clear the GAC of the assembly that contains the registration code and event hander class.
  2. Do an IISRESET.
  3. Uninstall the WSP.
  4. Do an IISRESET.
  5. Install the WSP.
  6. Do an IISRESET.

To get it working I never did a reboot or restarted SharePoint services, but I had done those prior to getting it working (before changing my code).

I suppose I could dig more with Reflector to see what I can find, but I believe you get to a dead end (unmanaged code) pretty quick. I wonder what could be holding on to the old DLL? I can't imagine that SQL Server would be in some way. Even so, a reboot would have fixed that (the entire farm, including SQL Server are on the same machine in this environment).


Solution

  • So, it appears that the whole problem was creating the event receiver by providing the GUID.

    EventReceiverDefinition eventReceiver = elevatedWeb.EventReceivers.Add(new Guid(MyEventReciverId));
    

    Now I am doing:

    EventReceiverDefinition eventReceiver = elevatedWeb.EventReceivers.Add();
    

    Unfortunately this means when I want to find out if the event is already registered, I must do something like the code below instead of a single one liner.

    // Had to use the below instead of: web.EventReceivers[new Guid(MyEventReceiverId)]
    private SPEventReceiverDefinition GetWebDeletingEventReceiver(SPWeb web)
    {
        Type eventReceiverType = typeof(MyEventHandler);
        string eventReceiverAssembly = eventReceiverType.Assembly.FullName;
        string eventReceiverClass = eventReceiverType.FullName;
    
        SPEventReceiverDefinition eventReceiver = null;
    
        foreach (SPEventReceiverDefinition eventReceiverIter in web.EventReceivers)
        {
            if (eventReceiverIter.Type == SPEventReceiverType.WebDeleting)
            {
                if (eventReceiverIter.Assembly == eventReceiverAssembly && eventReceiverIter.Class == eventReceiverClass)
                {
                    eventReceiver = eventReceiverIter;
                    break;
                }
            }
        }
    
        return eventReceiver;
    }
    

    It's still not clear why things seemed to linger and require some cleanup (iisreset, reboots, etc.) so if anyone else has this problem keep that in mind.