2

I need to attach an event receiver to a content type created by SharePoint:

        FolderCT.EventReceivers.Add(SPEventReceiverType.ItemAdding, SharepointReceiversAssemblyName, CTReceiverName);
        FolderCT.Update();

Unfortunately I get the error:

Folder at "/" is sealed

So, to go around the problem - I'm running the whole snippet under elevated privileges, and I'm setting Sealed to false. Is this a bad idea?

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            SPContentType FolderCT = web.ParentWeb.ContentTypes[SPBuiltInContentTypeId.Folder];
            FolderCT.Sealed = false;
            FolderCT.Update();

            FolderCT.EventReceivers.Add(SPEventReceiverType.ItemAdding, SharepointReceiversAssemblyName, CTReceiverName);
            FolderCT.Update();

            FolderCT.Sealed = true;
            FolderCT.Update();
        });

1 Answer 1

4

As long as you reseal it again, as you do, I do not see a problem.

Here's a snippet regarding the purpose of sealing:

Sometimes, you want to make absolutely sure that someone can’t go in and change your content type via the Windows SharePoint Services user interface. If an administrator were to delete or rename columns in your content type this can cause all sorts of issues as things may or may not be prorogated down to the child types. You might also want to ensure that a content type is only created / modified by using a feature and CAML. To solve this, a content type can be marked as sealed and it can only be marked as sealed through the object model. This prevents any casual changes to the content type via the user interface.

from http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/03/11/how-to-seal-a-content-type.aspx

Your Answer

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

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