0

I am using the below code for deleting the event receiver

It works fine for users in admin group, but for users in contributors group i am trying to delete the event receiver by using elevated permission, but it is not deleting the event receiver

private void RemoveEventReceiver(SPList list, bool IsRemove)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        if (!IsRemove)
                        {
                        foreach (SPEventReceiverDefinition receiver in list.EventReceivers)
                            {
                                if (receiver.Type == SPEventReceiverType.ItemUpdating)
                                {
                                    web.AllowUnsafeUpdates = true;
                                    try
                                    {
                                        receiver.Delete();
                                        list.Update();
                                    }
                                    catch (Exception ex)
                                    {
                                      //it comes to exception after receiver.Delete() line
                                    }
                                    break;
                                }
                            }
                        }

                    }
                }
            });
        }

What is the issue here ? i am not able to figure out the reason

2
  • What exception is thrown?
    – user2536
    Commented Jun 30, 2015 at 10:55
  • Error:Access Denied Exception showed - unable to evaluate expression because the code is optimized and it had system.threading.threadabortexception Commented Jun 30, 2015 at 10:59

3 Answers 3

1

It's mostly likely because you already have an instance of the SPList (from another context) before you try to execute the deletion of an EventReceiver. That's why the ThreadAbortionException is thrown.

Instead of sending in the list, send its name instead and get the list inside the RunWithElevatedPrivileges codeblock.

private void RemoveEventReceiver(string listName, bool isRemove)
{
    // No need to continue if it's FALSE
    if (isRemove) return;

    SPSecurity.RunWithElevatedPrivileges(() => 
    {
        using (var site = new SPSite(SPContext.Current.Site.ID))
        {
            using (var web = site.OpenWeb())
            {
                // Get the list with elevated permissions
                var list = web.Lists.TryGetList(listName);

                try
                {
                    web.AllowUnsafeUpdates = true;

                    foreach (SPEventReceiverDefinition eventReceiver in list.EventReceivers)
                    {
                        if (eventReceiver.Type == SPEventReceiverType.ItemUpdating)
                        {
                            eventReceiver.Delete();
                        }
                    }

                    // Update the list after the iteration is complete.
                    list.Update();
                }
                catch(Exception ex)
                {
                    //...
                }
                finally 
                {
                    // Remember to set AllowUnsafeUpdates to false again when you're done.
                    web.AllowUnsafeUpdates = false;
                }
            }
        }
    });
}
2

I would try the following:

  • Get the List again from the elevated Web instead of the instance you pass to this method.

  • Try searching your eventreceiver additional by assembly or classname to make sure you don't delete some SharePoint internal Receiver.

  • finally{ web.AllowUnsafeUpdates = false;}

0

Also, it looks to me like you are trying to modify a collection while iterating through it. I would try a regular for/next loop going from (SPList.EventReceivers.Count - 1) to 0. And you do need to get the list again from an elevated web. Which you get from an elevated site (I always indicate the SystemAccount token specifically for clarity):

string ListName="<Title of List>";
SPSecurity.RunWithElevatedPrivileges(delegate () 
{
        using (SPSite site = new SPSite(web.Site.ID, SPUserToken.SystemAccount))
        {
            using (SPWeb elevatedWeb = site.AllWebs[web.ID])
            {
                elevatedWeb.AllowUnsafeUpdates = true;
                try
                { 
                    SPList list = elevatedWeb.Lists[ListName];
                    int numEventReceivers = list.EventReceivers.Count;
                    int topIndexOfEventReceivers = numEventReceivers - 1;
                    for (int i=topIndexOfEventReceivers;i>-1;i--)
                    {
                        SPEventReceiverDefinition eventRecieverDefinition = list.EventReceivers[i];
                        eventRecieverDefinition.Delete();
                    }
                    list.Update();
                }
                finally
                {
                    elevatedWeb.AllowUnsafeUpdates = false;
                }
            }
        }
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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