Creating a Mirror Media Folder for Content Node in Umbraco Programmatically

If you want to keep your Umbraco media section well-organized, the UmbraCare.DedicatedMediaFolder package is a game-changer. It provides an API to structure media folders without any hassle.

Prerequisites

Before starting, ensure that you have installed the UmbraCare.DedicatedMediaFolder package via NuGet:

Install-Package UmbraCare.DedicatedMediaFolder

This package provides an API for quickly creating dedicated media folders for content nodes in Umbraco.

Step 2: Building a Simple RenderController with Media Folder Integration

For demonstration, I'll create a simple controller and inject IDedicatedMediaFolderHelper.

This controller will be responsible for creating a dedicated media folder automatically when a page is accessed.

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using UmbraCare.DedicatedMediaFolder.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Web.Common.Controllers;

public class ContentPageController : RenderController
{
    private IDedicatedMediaFolderHelper _dedicatedMediaFolderHelper;
    
    public ContentPageController(
        ILogger<ContentPageController> logger, 
        ICompositeViewEngine compositeViewEngine, 
        IUmbracoContextAccessor umbracoContextAccessor, 
        IDedicatedMediaFolderHelper dedicatedMediaFolderHelper) 
        : base(logger, compositeViewEngine, umbracoContextAccessor)
    {
        _dedicatedMediaFolderHelper = dedicatedMediaFolderHelper;
    }
    
    public override IActionResult Index()
    {
        CreateMirrorMediaFolderForContent(CurrentPage);
        
        return CurrentTemplate(CurrentPage);
    }

    private void CreateMirrorMediaFolderForContent(IPublishedContent currentPage)
    {
        int contentId = currentPage.Id;
        
        var mediaFolder = _dedicatedMediaFolderHelper.Get(contentId, createIfNotExists: true);
        
        if (mediaFolder != null)
        {
            Console.WriteLine($"Media folder created: {mediaFolder.Name}");
            Console.WriteLine($"Media ID: {mediaFolder.Id}");
            Console.WriteLine($"Media Path: {mediaFolder.Path}");
            Console.WriteLine($"Media Type: {mediaFolder.ContentType.Alias}");
        }
        else
        {
            Console.WriteLine("Failed to create or retrieve media folder.");
        }
    }
}

Step 3: Verifying the Created Mirror Media Folder

  1. Visit the URL of the content page in your browser.

  2. The RenderController will execute and automatically create a corresponding media folder in the Umbraco backoffice.

Debugging: Dedicated Media Folder was created successfully for content node

Debugging: The Mirror Media Folder was created successfully for the content node

  1. Navigate to the Media section in Umbraco.

  2. You should see a newly created media folder that mirrors the content page.

Comparing Umbraco Media vs Content Tree View

Comparing Umbraco Media vs Content Tree View

How IDedicatedMediaFolderHelper Simplifies Media Management

Here's a quick overview of IDedicatedMediaFolderHelper's capabilities:

Method Description
IMedia Get(int contentId, bool createIfNotExists, bool assignByRelation = true); Fetches the media folder linked to a content node by its ID. Can create it automatically if needed.
IMedia Get(IContent content, bool createIfNotExists, bool assignByRelation = true); Retrieves the media folder for a given IContent instance, with an option to create it if necessary.
IMedia Get(IEnumerable<string> expectedMediaTreeFolderNames, bool createIfNotExists); Creates or retrieves a structured media folder based on a predefined folder structure.
bool TryGetByRelation(int contentId, out IMedia dedicatedMediaFolder); Attempts to find a media folder using relations.

Understanding Scoped Dependencies in IDedicatedMediaFolderHelper

By default, IDedicatedMediaFolderHelper is registered as a scoped service in Umbraco.

This means that it is instantiated once per request and disposed of when the request ends.

builder.Services.AddScoped<IDedicatedMediaFolderHelper, DedicatedMediaFolderHelper>();

In more advanced scenarios, such as automated migrations from an old CMS, you might need to retrieve IDedicatedMediaFolderHelper manually via IServiceProvider.

This allows you to create media folders automatically outside the standard request scope.

Here is a pseudo-code:

using (var scope = _serviceProvider.CreateScope())
{
    var mediaFolderHelper = scope.ServiceProvider.GetRequiredService<IDedicatedMediaFolderHelper>();
    
    foreach (var legacyContent in legacyContentList)
    {
        var mediaFolder = mediaFolderHelper.Get(legacyContent.Id, createIfNotExists: true);
        Console.WriteLine($"Migrated media folder: {mediaFolder.Name} for legacy content node ID {legacyContent.Id} from the old CMS");
    }
}

Conclusion

The Dedicated Media Folder package is a complete solution for structuring the media library in Umbraco.

Especially for enterprise-level projects with thousands of pages, where keeping media assets structured is critical for performance and maintainability.

As a developer, you gain access to a fully-featured API that offers precise control over folder creation and management.

Meanwhile, editors can use an intuitive UI to create folders with just a single click.

💡For a detailed overview of its key features, watch the demonstration video below:

📽️ Video URL: https://www.youtube.com/embed/X5JSkJaag4E

What's Next?

If you haven't tried it, now's the perfect time to install the UmbraCare.DedicatedMediaFolder free version.

The free version has small limitations, while the full version requires a license purchase from UmbraCare.

↑ Top ↑