Search code examples
sharepointeventsmosscontent-typeevent-receiver

SharePoint: Error on unregistering an EventReceiver form a ContentType


I've got some problems unregistering some eventreceivers form a contenttype. The contenttype and the receivers were deployed and registered by myself so I don't try to remove any MOSS built-in or internal eventreceivers.

I trying to archive this with the following code snippet:

using (SPSite site = new SPSite("http://wssdev06/"))
        {
            using (SPWeb web = site.RootWeb)
            {
                // web.AllowUnsafeUpdates = true;

                SPContentType type = web.AvailableContentTypes[<ContentTypeName>];

                while (type.EventReceivers.Count > 0)
                {
                    type.EventReceivers[0].Delete();                        
                }

                type.Update();

                // web.AllowUnsafeUpdates = false;
            }
        }

Unfortunately the command "type.Update()" throws an exception telling me that the collection cannot be modified. As you can see in the code I've already tried different things to solve this problem, as allowing unsafe updates or running this code with elevated privileges. But I always get the same exception.

So what am I doing wrong?


Solution

  • Your problem is that the "AvailableContentTypes" property returns you a READ-ONLY collection.

    You should also use the 'ContentTypes' property, ans everything should be fine.

    Regards,

    Dug.