Orchard Hungary is an archive

This site is now an archive. We'll keep it so the content is accessible, but we won't update it anymore. However, we frequently publish Orchard-related news on our other site Orchard Dojo, so be sure to check that out instead. And if you're interested in news about our Orchard development company, Lombiq, check out our website.

Creating a hidden content part

Tags: development, content part, hidden

You might want to have a content part hidden: that is, you not only don't want users to be able to attach it to content types but you don't even want user to see - and thus know about - it. This is the case if the part is something very technical or if it's only interesting for a single content type.

The latter case, if you only want a content part to be attached to a single content type and thus don't want it to show anywhere else is simple: it's a much-used and fully supported scenario. You just have to name your part exactly like the content type. E.g. if your content type is "Page", just name your part "Page" and problem solved: this content part will be not listed among the parts under Content Definition.

The other case is slightly more complicated and although supported is somewhat hidden: so you don't want users to know about a content part because e.g. you only want to attach it from code to specific content types in certain scenarios. The key is that the content part definition's record in the DB has a column called Hidden. Nothing as far as I know actually uses it but if you set it to True for your part your part will be visually hidden. Setting it is simple but you need to access the table on the repository level, like this:

public class PartHider
        private readonly IRepository<ContentPartDefinitionRecord> _partDefinitionRepository;

        public PartHider(IRepository<ContentPartDefinitionRecord> partDefinitionRepository)
            _partDefinitionRepository = partDefinitionRepository;

        public void HidePart(string name)
            var partDefinition = _partDefinitionRepository.Fetch(p => p.Name == name).FirstOrDefault();
            partDefinition.Hidden = true;

This has no check on the existence of the part definition record but you get the idea.

Happy hiding your content parts!


  • remesq said Reply

    I know this is an old post, but I am trying to figure out how to hide a content part based on permissions. My scenario: I have many custom content types for clients, but attorneys and partners can add information to other fields contained in parts specific to them. I don't want to create three content types for "document", rather I want the client to see only relevant stuff, then attorney/partner to add to it as needed. I read that ShapeTableProvider might be the place for this, but not sure. Any thoughts? Thanks!

  • Piedone said Reply

    Then the technique explained in this post is definitely not for you (since it's about permanently hiding parts from the content definition editor).

    If you developed the content parts or fields yourself you could simply not return the shapes from their drivers based on permissions. If not then you can conditionally hide shapes (in a similar way you'd do from Placement.info) from an IShapeDisplayEvents.Displaying() implementation by setting the Position property to "-".

  • remesq said Reply

    Hey, thanks for responding. I figured that this would not be the technique to use. I was really just looking for the logic to put into a ShapeDisplayingContext or ShapeTableBuilder method. I put up a question on StackOverflow: http://stackoverflow.com/questions/29423023/trying-to-hide-content-parts-on-content-type
    Again, thanks.