Creating a Content Folder that only allows specific Content Types

If you need to organize the content among Assets, such as blocks and media, you can do this by creating your own Content Types that inherits from ContentFolder.

Unfortunately the Episerver UI only supports creating Content Folders in the folder section of the Asset pane, so you’ll need to find a way to create these new folders. If it’s a strict structure it’s very easy using the ContentRepository in Episerver. But if the editor should create them you’ll probably need to create a separate view for this or do some Dojo tweaks.

I might look more into the first option using some inspiration from Fredrik Vig‘s “Instant Templates“.

But for now, let’s focus on creating folders that only allows specific types.

Cut to the code

First of all, I need some Content Types that represents my folders – one that only allows Teaser Blocks and one that only allows Image Files, see Alloy Templates to look further into these two Content Types.

[ContentType(GUID = "964063f0-613b-4ce5-85a0-2d11fee0b905")]
[AvailableContentTypes(Availability.Specific, Include = new[] { typeof(TeaserBlock) })]
public class TeaserContentFolder : ContentFolder
{
}

[ContentType(GUID = "e730c883-f9b7-4d5e-b309-a00070159771")]
[AvailableContentTypes(Availability.Specific, Include = new[] { typeof(ImageFile) })]
public class ImageContentFolder : ContentFolder
{
}

At startup I will make sure that these two folders are created using an Initialization Module:

[InitializableModule]
[ModuleDependency(typeof(Web.InitializationModule))]
public class RestrictedContentFolderInitializationModule : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        IContentRepository contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();

    ContentReference siteAssetFolder = ContentReference.SiteBlockFolder;
        IEnumerable<ContentFolder> children = contentRepository.GetChildren<ContentFolder>(siteAssetFolder).ToList();

        CreateSpecificFolder<TeaserContentFolder>(children, "Teaser folder", siteAssetFolder, contentRepository);
        CreateSpecificFolder<ImageContentFolder>(children, "Image folder", siteAssetFolder, contentRepository);
    }

    public void Uninitialize(InitializationEngine context)
    {
    }

    private void CreateSpecificFolder<T>(IEnumerable children, string teaserFolderName, ContentReference siteAssetFolder, IContentRepository contentRepository) where T : IContent
    {
        if (children.Any(child => child.Name == teaserFolderName))
        {
            return;
        }

        var teaserFolder = contentRepository.GetDefault<T>(siteAssetFolder);
        teaserFolder.Name = teaserFolderName;
        contentRepository.Save(teaserFolder, SaveAction.Publish, AccessLevel.NoAccess);
    }
}

This will make sure that you’ll have two folders in the Assets Pane.

asset folders

So when I try to create any Blocks in the Teaser folder, I’ll only be able to create a Teaser block, and as usual when I’m only able to select one Content Type, it will automatically select it without giving me a list of available Content Types.

create teaser block

And when I try to upload files to the Image folder, I will only be able to upload files of that specific Media Type. Which in this example means, only images and not text files.

upload files

And when I try to create a Block in the Image folder, I will not be able to select any Block Types since none are available.

The code is also available on the GitHub Repository https://github.com/alfnilsson/RestrictedContentFolders

If you’re a Dojo Guru and have a way to tweak Episerver UI to allow the editor select which type of folder to create, please hit me up!

Leave a comment