Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11508

How to remove event handlers for a content type currently in use?

$
0
0

Hi,

We had a SP 2007 solution that managed event handlers as described in Brian Wilson's blog regarding event handlers. We then did a in-place upgrade to SP 2010. It so happend that we wanted to remove some of our old event handlers and this is where our problem started. We managed to delete event handlers(SPEventReceiverDefinitions) for Site and List by using the ui from Brian Wilsons feature "Manage Event Handlers" (ref the link above), but not anyone at all forContent Types... It simply wouldn't be deleted(remove was grayed out in the ui)

We then tried to do it by code. Below is a code snippet illustrating how we tried to delete the event handlers for content types:

using (SPWeb web = properties.Feature.Parent as SPWeb)
      {string targetClassName = "targetClassName.";
        web.AllowUnsafeUpdates = true;// Removing Content Type event handlersforeach (SPContentType ct in web.ContentTypes)
        {for (int i = ct.EventReceivers.Count - 1; i >= 0; i--)
          {if (ct.EventReceivers[i].Class.StartsWith(targetClassName))
            {
              ct.EventReceivers[i].Delete();
            }
          }
          ct.Update(true);
        }

        web.Update();
      }

The Content Types arenot sealed and arenot readonly. When debugging, we can see that theDelete() method are called on one of the Content Types we wanted to delete event handlers for. We noted that thect.EventReceivers.Count remains the same, before and after Delete() is called. We did not get any exceptions when running this code.

When running the code a second time and debugging again, we see that the very same Content Type still has the very same event handler attached... As in, it wasn't deleted....

We also tried to delete the event handlers through PowerShell as described here in the post by Per Jakobsen. However, the script did not really seem to find any EventReceivers... We tried to write the$site.AllWebs | % {$_.Lists} | % {$_.ContentTypes} | % {$_.EventReceivers} list to file, but it was empty.. We did however get a long list when writing $site.AllWebs | % {$_.Lists} | % {$_.ContentTypes} to file. We could then in that file see the event handler references we want to remove registered to our Content Types, as we did during code debugging earlier on. So PowerShell might still be the way to go here...

So, does anyone know if there is a way to force this delete through, either by code, PowerShell or some other means? Any help regarding this matter would be very much appreciated :)

 


Viewing all articles
Browse latest Browse all 11508

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>