CMS, EPiServer, Episerver 11

How to hide content types from editors in Episerver which you don’t own

In this blog post I’ll show you two ways how to hide content types which you don’t own (meaning the content types are coming from Episerver or 3rd parties). Simple no coding needed and with an initialization module.

Sometimes you might have a need to hide content types from editors but the problem is that you don’t own and have control over the content type definition. If you own the content and don’t want it to be available for editors then it is a simple value that you set in the ContentTypeAttribute, AvailableInEditMode = false.

Why would you have need to hide some content types? For example if you are using Episerver Find it currently also adds the ‘Customized search block’ (there is a request to have this block in a separate NuGet package, so in the future this point might not be valid anymore) but if you have disabled asp.net session this block will not work because it currently requires the session tempdata to store something. So now you want to hide this one from editors so that they don’t get confused when it doesn’t work but is available.

Another case when you might want to hide content types from editors is that if you use Episerver Forms and have created custom Forms elements and you don’t want to have the default implementations to be available. Another case might be that you just want to hide some of the default elements like the Captcha implementation (Captcha requires session state but Recaptcha doesn’t) because you don’t have session state enabled.

See the documentation about asp.net session state and Episerver. Episerver has a sample Episerver Forms implementation available as NuGet package (includes the Recaptcha) and the source code is available in GitHub.

Hide content type without code

The simplest way to hide a content type from editors without any need for coding is to login to your Episerver instance and navigate to admin view -> Content Type tab. Find the content type that you want to hide from editors and click the content type name in the left panel to select it -> Click the content type ‘Settings’ -> Uncheck the checkbox ‘Available in edit view’ and click save. Content type is no longer available for editors. Remember that you can always revert this change in admin by clicking the ‘Revert to default’ button when viewing a content types settings.

Downside of this is that you need to remember to do this in all of your environments and the setting might change when a NuGet package gets updated.

Hide content type using an intialization module

Another way to hide content types from editors is to do it programmatically using an Episerver initialization module. The module is quite simple: get IContentTypeRepository from the InitializationEngine context, get a content type using its Guid, create a writable clone and set the IsAvailable = false and save using the IContentTypeRepository.

Question is how to get/know the content type Guid? Simple way is to go to Episerver admin view -> Content Type tab -> Select a content type and view its settings.

hide-customized-search-blockhide-forms-captcha-block

In the images I have highlighted the content type Guids (you could also use the name, but I’ll use Guids). So this is the easiest way to find out the Guids of the content types another way would be to use your favorite .Net decompiler and find the content types from code and look the at the ContentTypeAttribute values (why would you do it this way? :D).

Next is the code for the initialization module which hides the content type from editors using the Guid (Gist url for the code).

Note! Your initialization module code should take dependency to the modules that “add” these content types you are modifying. In the sample I’ve taken dependency to the ‘EPiServer.Web.InitializationModule’ and ‘EPiServer.Forms.InitializationModule’ to make sure this module is executed after those. It might be enough to have only dependency to ‘EPiServer.Web.InitializationModule’ as the content types should be already synched at this stage (cheat sheet of intialization modules).

So there you have the initialization module to hide content types from your editors.

But there must be other ways too?

Well yes, there are. There are at least two threads at Episerver World where people have asked how to hide the Episerver Forms container or the default Forms elements when custom elements are created.

The presented solution works BUT it uses the DefaultContentTypeAvailablilityService as a base class which is in Episerver internal namespace ‘EPiServer.DataAbstraction.Internal’. The internal namespace indicates that it is not part of semantic versioning and could change without any notice. So be carefull when using something from internal namespaces. I would rather use this initialization module approach 😉

But one thing to note here is that the big difference with having initialization module vs custom content type availability service is that initialization module hides the content type from ALL users but with custom content type availability service you could use the principal object to hide content types per user or role (ListAvailable methods). The default implementation queries create access right for the user to the content.

So if the ‘DefaultContentTypeAvailablilityService’ uses users create access rights to decide is the content type availabe (for the user) it means we have another no coding required option to hide content types or create an initialization module that removes the create content access right from everyone or modifies it so that certain roles only can create the content type.

So I think you should not have the need to create a custom implementation of ‘ContentTypeAvailabilityService’ to just hide content types from ALL users or certain users/roles because you can do that by modifying the content type availability or by configuring the create access rights for the content.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s