Umbraco CMS
CloudHeartcoreDXPMarketplace
14.latest
14.latest
  • Umbraco CMS Documentation
  • Legacy Documentation
    • Our Umbraco
    • GitHub
  • Release Notes
  • Contribute
  • Sustainability Best Practices
  • Fundamentals
    • Get to know Umbraco
    • Setup
      • Requirements
      • Installation
        • Install using .NET CLI
        • Running Umbraco in Docker using Docker Compose
        • Install using Visual Studio
        • Local IIS With Umbraco
        • Install using Visual Studio Code
        • Installing Nightly Builds
        • Running Umbraco on Linux/macOS
        • Unattended Installs
      • Upgrade your project
        • Version Specific Upgrades
          • Upgrade from Umbraco 8 to the latest version
          • Migrate content to Umbraco 8
          • Minor upgrades for Umbraco 8
          • Upgrade to Umbraco 7
          • Minor upgrades for Umbraco 7
      • Server setup
        • Running Umbraco On Azure Web Apps
        • Hosting Umbraco in IIS
        • File And Folder Permissions
        • Runtime Modes
        • Umbraco in Load Balanced Environments
          • Load Balancing Azure Web Apps
          • Standalone File System
          • Advanced Techniques With Flexible Load Balancing
          • Logging With Load Balancing
    • Backoffice
      • Sections
      • Property Editors
        • Built-in Property Editors
          • Checkbox List
          • Collection
          • Color Picker
          • Content Picker
          • Document Picker
          • DateTime
          • Date
          • Decimal
          • Email Address
          • Eye Dropper Color Picker
          • File Upload
          • Image Cropper
          • Label
          • Markdown Editor
          • Media Picker
          • Member Group Picker
          • Member Picker
          • Multi Url Picker
          • Repeatable Textstrings
          • Numeric
          • Radiobutton List
          • Slider
          • Tags
          • Textarea
          • Textbox
          • Toggle
          • User Picker
          • Block Editors
            • Block Grid
            • Block List
          • Dropdown
          • Rich Text Editor
            • Rich Text Editor Configuration
            • Rich Text Editor Styles
            • Rich Text Editor Plugins
            • Blocks in Rich Text Editor
      • Login
      • Document Blueprints
      • Sidebar
      • Log Viewer
      • Language Variants
      • Settings Dashboards
    • Data
      • Defining Content
        • Default Document Types
        • Document Type Localization
      • Creating Media
        • Default Data/Media Types
      • Members
      • Data Types
        • Default Data Types
      • Scheduled Publishing
      • Using Tabs
      • Users
      • Relations
      • Dictionary Items
      • Content Version Cleanup
    • Design
      • Templates
        • Basic Razor Syntax
        • Razor Cheatsheet
      • Rendering Content
      • Rendering Media
      • Partial Views
      • Stylesheets And JavaScript
    • Code
      • Service APIs
      • Subscribing To Notifications
      • Creating Forms
      • Debugging
        • Logging
      • Source Control
  • Implementation
    • Learn how Umbraco works
    • Routing
      • Controller & Action Selection
      • Execute Request
      • Request Pipeline
    • Custom Routing
      • Adding a hub with SignalR and Umbraco
    • Controllers
    • Data Persistence (CRUD)
    • Composing
    • Integration Testing
    • Nullable Reference Types
    • Services and Helpers
      • Circular Dependencies
    • Unit Testing
  • Customizing
    • Extend and customize the editing experience
    • Project Bellissima
    • Setup Your Development Environment
      • Vite Package Setup
    • Foundation
      • Working with Data
        • Repositories
        • Context API
        • Store
        • States
      • Contexts
        • Property Dataset Context
      • Umbraco Element
        • Controllers
          • Write your own controller
      • Sorting
      • Routes
      • Icons
      • Backoffice Localization
      • Terminology
    • Extension Overview
      • Extension Registry
        • Extension Registration
        • Extension Manifest
      • Extension Types
        • Menu
        • Header Apps
        • Icons
        • Modals
          • Confirm Dialog
          • Custom Modals
          • Route Registration
        • Bundle
        • Kind
        • Backoffice Entry Point
        • Extension Conditions
        • Dashboards
        • Entity Actions
        • Entity Bulk Actions
        • Entity Create Option Action
        • Trees
        • Global Context
        • Section Sidebar
        • Section View
        • Sections
        • Workspace Context
        • Workspace Views
        • Workspace Actions
        • Localization
      • Extension Kind
      • Extension Conditions
      • Custom Extension types
    • Sections & Trees
    • Searchable Trees (ISearchableTree)
    • Property Editors
      • Property Editors Composition
        • Property Editor Schema
        • Property Editor UI
      • Property Value Converters
      • Property Actions
      • Integrate Property Editors
      • Tracking References
      • Content Picker Value Converter Example
      • Property Dataset
      • Integrate Validaction
    • Workspaces
    • Umbraco Package
    • UI Library
  • Extending
    • Build on Umbraco functionality
    • Health Check
      • Health Check Guides
        • Click-Jacking Protection
        • Content Content Security Policy (CSP)
        • Content/MIME Sniffing Protection
        • Cross-site scripting Protection (X-XSS-Protection header)
        • Debug Compilation Mode
        • Excessive Headers
        • Fixed Application Url
        • Folder & File Permissions
        • HTTPS Configuration
        • Notification Email Settings
        • SMTP
        • Strict-Transport-Security Header
    • Language Files & Localization
      • .NET Localization
    • Backoffice Search
    • Creating a Custom Database Table
    • Embedded Media Providers
    • Custom File Systems (IFileSystem)
      • Using Azure Blob Storage for Media and ImageSharp Cache
    • Configuring Azure Key Vault
    • Packages
      • Creating a Package
      • Language file for packages
      • Listing a Package on the Umbraco Marketplace
      • Good practice and defaults
      • Packages on Umbraco Cloud
      • Installing and Uninstalling Packages
      • Maintaining packages
      • Create accessible Umbraco packages
      • Example Package Repository
  • Reference
    • Dive into the code
    • Configuration
      • Basic Authentication Settings
      • Connection strings settings
      • Content Dashboard Settings
      • Content Settings
      • Data Types Settings
      • Debug settings
      • Examine settings
      • Exception filter settings
      • FileSystemProviders Configuration
      • Global Settings
      • Health checks
      • Hosting settings
      • Imaging settings
      • Indexing settings
      • Install Default Data Settings
      • Logging settings
      • Maximum Upload Size Settings
      • Models builder settings
      • NuCache Settings
      • Package Migration
      • Plugins settings
      • Request handler settings
      • Runtime settings
      • Security Settings
      • Serilog settings
      • Type finder settings
      • Unattended
      • Web routing
    • Templating
      • Models Builder
        • Introduction
        • Configuration
        • Builder Modes
        • Understand and Extend
        • Using Interfaces
        • Tips and Tricks
      • Working with MVC
        • Working with MVC Views in Umbraco
        • View/Razor Examples
        • Using MVC Partial Views in Umbraco
        • Using View Components in Umbraco
        • Querying & Traversal
        • Creating Forms
      • Macros
    • Querying & Models
      • IMemberManager
      • IPublishedContentQuery
      • ITagQuery
      • UDI Identifiers
      • UmbracoContext helper
      • UmbracoHelper
      • IPublishedContent
        • IPublishedContent Collections
        • IPublishedContent IsHelpers
        • IPublishedContent Property Access & Extension Methods
    • Routing & Controllers
      • Custom MVC controllers (Umbraco Route Hijacking)
      • Custom MVC Routes
      • Custom Middleware
      • URL Rewrites in Umbraco
      • Special Property Type aliases for routing
      • URL Redirect Management
      • Routing in Umbraco
        • FindPublishedContentAndTemplate()
        • IContentFinder
        • Inbound request pipeline
        • Outbound request pipeline
        • Published Content Request Preparation
      • Surface controllers
        • Surface controller actions
      • Umbraco API Controllers
        • Porting old Umbraco API Controllers
    • Content Delivery API
      • Custom property editors support
      • Extension API for querying
      • Media Delivery API
      • Protected content in the Delivery API
      • Output caching
      • Property expansion and limiting
      • Additional preview environments support
    • Webhooks
      • Expanding Webhook Events
    • API versioning and OpenAPI
    • Searching
      • Examine
        • Examine Management
        • Examine Manager
        • Custom indexing
        • PDF indexes and multisearchers
        • Quick-start
    • Using Notifications
      • Notification Handler
      • CacheRefresher Notifications Example
      • ContentService Notifications Example
      • Creating And Publishing Notifications
      • Determining if an entity is new
      • MediaService Notifications Example
      • MemberService Notifications Example
      • Sending Allowed Children Notification
      • Umbraco Application Lifetime Notifications
      • EditorModel Notifications
        • Customizing the "Links" box
      • Hot vs. cold restarts
    • Inversion of Control / Dependency injection
    • Management
      • Using Umbraco services
        • Consent Service
        • Media Service
        • Relation Service
        • Content Service
        • Content Type Service
        • Localization Service
        • User Service
    • Plugins
      • Creating Resolvers
      • Finding types
    • Cache & Distributed Cache
      • Accessing the cache
      • ICacheRefresher
      • IServerMessenger
      • Getting/Adding/Updating/Inserting Into Cache
      • Examples
        • Working with caching
    • Response Caching
    • Security
      • API rate limiting
      • BackOfficeUserManager and Events
      • Cookies
      • Replacing the basic username/password check
      • External login providers
      • Locking of Users and password reset
      • Reset admin password
      • Umbraco Security Hardening
      • Umbraco Security Settings
      • Sensitive data
      • Sanitizing the Rich Text Editor
      • Setup Umbraco for a FIPS Compliant Server
      • HTTPS
      • Two-factor Authentication
      • Server-side file validation
    • Scheduling
    • Common Pitfalls & Anti-Patterns
    • API Documentation
    • Debugging with SourceLink
    • Language Variation
    • UmbracoMapper
    • Distributed Locks
    • Management API
      • Setup OAuth using Postman
    • Custom Swagger API
    • Umbraco Flavored Markdown
  • Tutorials
    • Overview
    • Creating a Basic Website
      • Getting Started
      • Document Types
      • Creating Your First Template
      • CSS and Images
      • Displaying the Document Type Properties
      • Creating a Master Template
      • Creating Pages and Using the Master Template
      • Setting the Navigation Menu
      • Articles and Article Items
      • Adding Language Variants
      • Conclusions
    • Creating your First Extension
    • Creating a Custom Dashboard
      • Adding localization to the dashboard
      • Adding functionality to the Dashboard
      • Using Umbraco UI library in the Dashboard
    • Creating a Property Editor
      • Adding configuration to a Property Editor
      • Integrating context with a Property Editor
      • Custom value conversion for rendering
      • Adding server-side validation
        • Default Property Editor Schema aliases
    • Creating a Multilingual Site
    • Add Google Authentication (Users)
    • Add Microsoft Entra ID authentication (Members)
    • Creating Custom Database Tables with Entity Framework
    • The Starter Kit
      • Lessons
        • Customize the Starter Kit
        • Add a Blog Post Publication Date
          • Add a Blog Post Publication Date
          • Add a Blog Post Publication Date
        • Add Open Graph
          • Add Open Graph - Step 1
          • Add Open Graph - Step 2
          • Add Open Graph - Step 3
          • Add Open Graph - Step 4
          • Add Open Graph - Summary
        • Ask For Help and Join the Community
    • Editor's Manual
      • Getting Started
        • Logging In and Out
        • Umbraco Interface
        • Creating, Saving and Publishing Content Options
        • Finding Content
        • Editing Existing Content
        • Sorting Pages
        • Moving a Page
        • Copying a Page
        • Deleting and Restoring Pages
      • Working with Rich Text Editor
      • Version Management
        • Comparing Versions
        • Rollback to a Previous Version
      • Media Management
        • Working with Folders
        • Working with Media Types
        • Cropping Images
      • Tips & Tricks
        • Refreshing the Tree View
        • Audit Trail
        • Notifications
        • Preview Pane Responsive View
        • Session Timeout
    • Multisite Setup
    • Member Registration and Login
    • Custom Views for Block List
    • Connecting Umbraco Forms and Zapier
    • Creating an XML Sitemap
    • Implement Custom Error Pages
    • Create a custom maintenance page
    • Creating a backoffice API
      • Documenting your controllers
      • Adding a custom Swagger document
      • Versioning your API
      • Polymorphic output in the Management API
      • Umbraco schema and operation IDs
      • Access policies
Powered by GitBook
On this page
Edit on GitHub
Export as PDF

Last updated 11 months ago

Customising the behaviour of an Umbraco Application at 'start up'. for example adding, removing, or replacing the core functionality of Umbraco or registering custom code to subscribe to notifications.

Overview

An Umbraco application is a Composition made of many different 'collections' and single items of specific functionality/implementation logic/components (eg. UrlProviders, ContentFinders - see below for a full list). These collections are populated when the Umbraco Application starts up.

'Composing' is the term used to describe the process of curating which pieces of functionality should be included in a particular collection. The code that implements these choices at start up is called a Composer.

A Component is a generic wrapper for writing custom code during composition, it has two methods: Initialize() and Terminate() and these are executed when the Umbraco Application starts up, and when it shuts down, respectively. The functionality of a Component is identical to having a class handling both the UmbracoApplicationStartingNotification and UmbracoApplicationStoppingNotification.

How are the collections populated? - Either by scanning the codebase for c# classes that inherit from a particular base class or implement a particular interface (typed scanned) or by being explicitly registered via a Composer.

Umbraco setup the default set of components and collections that deliver the core 'out of the box' Umbraco behaviour. These default collections can be removed, reordered, replaced, etc. by implementing IComposer's and IComponents to customise and extend Umbraco's behaviour.

Example - Creating a Composer to listen for ContentSavingNotification

This example shows how to create a component and a notification handler for the ContentSavingNotification, (perhaps to check for explicit words, or some custom business logic that needs to run before the content item is saved in Umbraco).

We create a new C# class that implements IComposer and use it register our notification handler.

Ordering of composers is important, the last one added can override a previously added composer! Make sure, when overriding, that your composer that is doing the overriding, is 'composing', after the composer has 'composed' the element you wish to override!

Example - Explicitly Registering a new custom OEmbedProvider

This example shows a custom 'Spotify' OEmbed Provider which will allow Spotify URLs to be used via the 'embed' button in the Rich Text Editors. As the collection for OEmbedProviders is not 'typed scanned', we need to explicitly register the provider in the collection of OEmbedProviders. We create a C# class which implements IComposer and append our new Spotify OEmbedProvider to the EmbedProvidersCollection:

See a list of collections below to determine which are 'type scanned' and which require explicit registration.

ComponentComposer

It's an implementation of IComposer, that provides a quicker way to add a custom component to the Component's collection. Creating a C# class that inherits from ComponentComposer<YourComponentType> will automatically add YourComponentType to the collection of Components. In the example above, the SubscribeToContentServiceSavingComposer for the SubscribeToContentServiceSavingComponent could have been written more conveniently as:

Collections

"Collections of elements", for example, the ContentFinders collection. - Collections are another concept that Umbraco uses to make things simpler, on top of DI. A collection builder builds a collection, allowing users to add and remove types before anything is registered into DI.

Below is a list of collections with their corresponding 'collection type' and how items for this collection 'out of the box' are registered.

Types of Collections

Example - Modifying Collections

This example shows how to control which Healthchecks are available to run in the Umbraco backoffice. Create a C# class which implements IComposer, the Compose method gives access to the HealthChecks collection of the Umbraco Composition - first we clear all HealthChecks from the collection, then add back in the ones we want to keep:

Attributes

Umbraco has some useful C# attributes to decorate your composer classes or Types used in collections, to give you further control on how and when your Composers will 'compose'.

ComposeBefore and ComposeAfter

A finer-grain mechanism can then be used to refine the order of composition. Each composer can specify that it should compose before or after another composer, using the ComposeBefore and ComposeAfter attributes. For instance:

ThisComposer will 'compose' before ThatOtherComposer.

If you create a circular dependency then Umbraco will fail to boot and will report the conflicting/circular dependency.

Weight

This attribute is used only for WeightedCollectionBuilders (see list above). It specifies an integer ordinal value for each item to be added to the weighted collection which controls their sort order. The weighting attribute is not applied to the Composers.

HideFromTypeFinder

This is used to hide a type from being auto-scanned/added to a collection as in some cases certain items/types may need to be added to a collection manually. For example, a Search package may make it optional whether to replace the 'backoffice search' with an ISearchableTree implementation. Type scanning would make this change automatically at start up if the custom implementation was detected via type scanning. This attribute could hide the class from the scanner.

DisableComposer & Disable

These attributes allow you to disable a particular implementation of a composer or class - Let's say Umbraco ships with two different ways of doing "something" (for instance, two front-end caches). Each way has its own composer, which registers all the relevant elements. Keep in mind that if both composers are detected, there will be some sort of collision. Ideally, we want to disable one of them. That can be achieved with the Disable attribute:

When used without arguments, these attributes apply to the composer they are marking. But, and this is where it becomes interesting, they can be used with an argument to act on another component. Therefore, should a user want to replace our "something" with theirs, they would write the following code:

But maybe they want to swap our two "something" implementations? In this case, assembly-level attributes can be used:

Umbraco also has [Enable] & [EnableComposer] attributes but all composers are enabled by default.

Runtime Levels

The Umbraco.Cms.Core.RuntimeLevel enum contains the following values:

BootFailed

The runtime has failed to boot and cannot run.

Unknown

The level is unknown.

Boot

The runtime is booting.

Install

The runtime has detected that Umbraco is not installed at all, ie. there is no database, and is currently installing Umbraco.

Upgrade

The runtime has detected an Umbraco install which needed to be upgraded, and is currently upgrading Umbraco.

Run

The runtime has detected an up-to-date Umbraco install and is running.

Example of using Ordered Collections and adding types explicitly

You may wish to create an Umbraco package that allows package consumers to extend and add additional functionality. In this example, we show how you can use the OrderedCollectionBuilderBase.

Example of using Lazy Collections with Type Scanning

You may wish to create an Umbraco package that allows package consumers to extend and add additional functionality. In this example, we show how you can use the LazyCollectionBuilderBase to scan assemblies that implement your interface by using the TypeLoader

Don't use type scanning if you can avoid it. Type scanning increases the Umbraco boot time.

If your use case requires type scanning, ensure your interface implements IDiscoverable. This marker interface ensures that types are scanned once and then cached by Umbraco. This way, we save time by not having to re-scan for types repeatedly.

Collection
Type
Registration
Method
Notes
Level
Description
using System.Linq;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Extensions;

namespace My.Website;

public class SubscribeToContentServiceSavingComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        builder.AddNotificationHandler<ContentSavingNotification, CustomContentSavingNotificationHandler>();
    }
}

public class CustomContentSavingNotificationHandler : INotificationHandler<ContentSavingNotification>
{
    public void Handle(ContentSavingNotification notification)
    {
        foreach (var content in notification.SavedEntities
            // Check if the content item type has a specific alias
            .Where(c => c.ContentType.Alias.InvariantEquals("MyContentType")))
        {
            // Do something if the content is using the MyContentType doctype
        }
    }
}
using Umbraco.Cms.Core.Media.EmbedProviders;
using Umbraco.Cms.Core.Serialization;

namespace My.Website;

public class SpotifyEmbedProvider : OEmbedProviderBase
{
    public SpotifyEmbedProvider(IJsonSerializer jsonSerializer)
        : base(jsonSerializer)
    {
    }

    public override string ApiEndpoint => "https://embed.spotify.com/oembed/";

    // Playlist
    // https://open.spotify.com/user/spotify/playlist/37i9dQZF1E4sNI4jZloSZr?si=cueBooBfTnqCGriSa4N_Kg
    // spotify:user:spotify:playlist:37i9dQZF1E4sNI4jZloSZr
    // Artist
    // https://open.spotify.com/artist/0iirUbtgwt9jEkc2Grin8C?si=TLeUR2cHR-KPRJJhW6YiVg
    // spotify:artist:0iirUbtgwt9jEkc2Grin8C
    // Album
    // https://open.spotify.com/album/0lvtdqkqIln6uDBBUT7DHL?si=XTVJIEmnS_OVv9l6ktPFiw
    // spotify:album:0lvtdqkqIln6uDBBUT7DHL
    // Track
    // https://open.spotify.com/track/7aCk4XfXIEJM2MecU6Gmf2?si=vESDzI0xTNeA9FQ_dvf1eQ
    // spotify:track:7aCk4XfXIEJM2MecU6Gmf2
    public override string[] UrlSchemeRegex => new[]
    {
        @".*.spotify.com/.*",
        @"spotify:.*"
    };

    public override Dictionary<string, string> RequestParams => new();

    public override string? GetMarkup(string url, int maxWidth = 0, int maxHeight = 0)
    {
        string requestUrl = base.GetEmbedProviderUrl(url, maxWidth, maxHeight);
        OEmbedResponse? oembed = base.GetJsonResponse<OEmbedResponse>(requestUrl);

        return oembed?.GetHtml();
    }
}
using Umbraco.Cms.Core.Composing;

namespace My.Website;

public class RegisterEmbedProvidersComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        // Change the EmbedProvidersCollection
        // by adding our new EmbedProvider for Spotify
        builder.EmbedProviders().Append<SpotifyEmbedProvider>();
    }
}
public class SubscribeToContentServiceSavingComposer : ComponentComposer<SubscribeToContentServiceSavingComponent>
{ }

Actions

Lazy

Type scanned for IAction

BackOfficeAssets

Ordered

Explicit Registration - Empty by default

CacheRefreshers

Lazy

Type scanned for ICacheRefresher

Components

Ordered

Explicit Registration

ContentApps

Ordered

Package.manifest & Explicit Registration

ContentFinders

Ordered

Explicit Registration

ContentIndexHandlers

Lazy

Type scanned for IContentIndexHandler

Dashboards

Weighted

Package.manifest & Explicit Registration

DataEditors

Lazy

Type scanned for IDataEditor

DataValueReferenceFactories

Ordered

Explicit Registration - Empty by default

EditorValidators

Lazy

Type scanned for IEditorValidator

EmbedProviders

Ordered

Explicit Registration

FilterHandlers

Lazy

Type scanned for IFilterHandler

HealthChecks

Lazy

Type scanned for HealthCheck

HealthCheckNotificationMethods

Lazy

Type scanned for IHealthCheckNotificationMethod

ManifestFilters

Ordered

Explicit Registration - Empty by default

ManifestValueValidators

Set

Explicit Registration

MapDefinitions

Set

Explicit Registration

Mappers

Set

Explicit Registration

MediaUrlGenerators

Set

Explicit Registration

MediaUrlProviders

Ordered

Explicit Registration

NPocoMappers

Set

Explicit Registration

PackageMigrationPlans

Lazy

Type scanned for PackageMigrationPlan

PartialViewSnippets

Lazy

Explicit Registration. Reads .cshtml files from Umbraco.Cms.Core.EmbeddedResources.Snippets assembly

PropertyValueConverters

Ordered

Type scanned for IPropertyValueConverter

RuntimeModeValidators

Set

Explicit Registration

SearchableTrees

Lazy

Type scanned for ISearchableTree

Sections

Ordered

Package.manifest & Explicit Registration

SelectorHandlers

Lazy

Type scanned for ISelectorHandler

SortHandlers

Lazy

Type scanned for ISortHandler

TourFilters

Base

Empty collection

Trees

Base

Type scanned. Must inherit TreeControllerBase & use [Tree]

UrlProviders

Ordered

Explicit Registration

UrlSegmentProviders

Ordered

Explicit Registration

Validators

Lazy

Explicit Registration

Set

SetCollectionBuilderBase

The base class for collection builders that do not order their items explicitly.

Ordered

OrderedCollectionBuilderBase

The base class for collection builders that order their items explicitly.

Weighted

WeightedCollectionBuilder

The base class for collection builders that order their items by the [Weight] attribute.

Lazy

LazyCollectionBuilderBase

The base class for collection builders that resolve the types at the last moment, only when the collection is required.

using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.HealthChecks.Checks.Permissions;
using Umbraco.Cms.Core.HealthChecks.Checks.Security;

namespace My.Website;

public class MyComposer: IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        // Remove all HealthChecks
        builder.HealthChecks().Clear();

        // Explicitly add back the ones we want to use
        builder.HealthChecks().Add<FolderAndFilePermissionsCheck>();
        builder.HealthChecks().Add<ExcessiveHeadersCheck>();
    }
}
[ComposeBefore(typeof(ThatOtherComposer))]
public class ThisComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
    }
}
using System;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Dashboards;

namespace Umbraco.Web.Dashboards;

[Weight(10)]
public class FormsDashboard : IDashboard
{
    public string Alias => "formsInstall";
    public string[] Sections => new [] { Constants.Applications.Forms };
    public string View => "views/dashboard/forms/formsdashboardintro.html";
    public IAccessRule[] AccessRules => Array.Empty<IAccessRule>();
}
[Disable]
public class Way2Composer : IComposer
{
    //...
}
[Disable(typeof(Way1Composer))]
public class MyComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        // ...
    }
}
[assembly:DisableComposer(typeof(Way1Composer))]
[assembly:EnableComposer(typeof(Way2Composer))]

BootFailed

The runtime has failed to boot and cannot run.

Unknown

The level is unknown.

Boot

The runtime is booting.

Install

The runtime has detected that Umbraco is not installed at all, ie. there is no database, and is currently installing Umbraco.

Upgrade

The runtime has detected an Umbraco install that needed to be upgraded and is currently upgrading Umbraco.

Run

The runtime has detected an up-to-date Umbraco install and is running.

using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Management.Controllers;
using Umbraco.Cms.Api.Management.Routing;
using Umbraco.Cms.Core.Composing;

namespace UmbracoDocs.Samples;

public interface IDoThing
{
    string DoTheThing(string message);
}

public class FirstThing : IDoThing
{
    public string DoTheThing(string message)
        => $"First: {message}";
}

public class SecondThing : IDoThing
{
    public string DoTheThing(string message)
        => $"Second: {message}";
}

public class ThirdThing : IDoThing
{
    public string DoTheThing(string message)
        => $"Third: {message}";
}

// OrderedCollection - use when order of items is important (You may want to execute them in order)
public class DoThingsCollectionBuilder : OrderedCollectionBuilderBase<DoThingsCollectionBuilder, DoThingsCollection, IDoThing>
{
    protected override DoThingsCollectionBuilder This => this;
}

public class DoThingsCollection : BuilderCollectionBase<IDoThing>
{
    public DoThingsCollection(Func<IEnumerable<IDoThing>> items)
        : base(items)
    {
    }
}

public class DoThingsComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        // Explicitly add to the collection a Type in a specific order
        builder
            .WithCollectionBuilder<DoThingsCollectionBuilder>()
            .Append<FirstThing>()
            .Append<SecondThing>()
            .Append<ThirdThing>();
    }
}

// An Umbraco Management API Controller - used in a Dashboard or Property Editor, perhaps?
[ApiExplorerSettings(GroupName = "Do things")]
[VersionedApiBackOfficeRoute("do/things")]
public class DoThingsController : ManagementApiControllerBase
{
    private readonly DoThingsCollection _doThings;

    public DoThingsController(DoThingsCollection doThings)
        => _doThings = doThings;

    [HttpGet]
    [ProducesResponseType<string[]>(StatusCodes.Status200OK)]
    public IActionResult DoAllThings(string message)
    {
        var allThingsDone = _doThings
            .Select(doThing => doThing.DoTheThing(message))
            .ToArray();

        return Ok(allThingsDone);
    }
}
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Management.Controllers;
using Umbraco.Cms.Api.Management.Routing;
using Umbraco.Cms.Core.Composing;

namespace UmbracoDocs.Samples;

// Implement `IDiscoverable` to boost scanning performance
public interface IDoThing : IDiscoverable
{
    string DoTheThing(string message);
}

public class FirstThing : IDoThing
{
    public string DoTheThing(string message)
        => $"First: {message}";
}

public class SecondThing : IDoThing
{
    public string DoTheThing(string message)
        => $"Second: {message}";
}

public class ThirdThing : IDoThing
{
    public string DoTheThing(string message)
        => $"Third: {message}";
}

public class DoThingsCollectionBuilder : LazyCollectionBuilderBase<DoThingsCollectionBuilder, DoThingsCollection, IDoThing>
{
    protected override DoThingsCollectionBuilder This => this;
}

public class DoThingsCollection : BuilderCollectionBase<IDoThing>
{
    public DoThingsCollection(Func<IEnumerable<IDoThing>> items)
        : base(items)
    {
    }
}

public class DoThingsComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        // Add types from assemblies. Be conscious of using type scanning, as this adds to the Umbraco boot time.
        // If you need to use type scanning, ensure that your interface implements `IDiscoverable`.
        builder
            .WithCollectionBuilder<DoThingsCollectionBuilder>()
            .Add(() => builder.TypeLoader.GetTypes<IDoThing>());
    }
}

// An Umbraco Management API Controller - used in a Dashboard or Property Editor, perhaps?
[ApiExplorerSettings(GroupName = "Do things")]
[VersionedApiBackOfficeRoute("do/things")]
public class DoThingsController : ManagementApiControllerBase
{
    private readonly DoThingsCollection _doThings;

    public DoThingsController(DoThingsCollection doThings)
        => _doThings = doThings;

    [HttpGet]
    [ProducesResponseType<string[]>(StatusCodes.Status200OK)]
    public IActionResult DoAllThings(string message)
    {
        var allThingsDone = _doThings
            .Select(doThing => doThing.DoTheThing(message))
            .ToArray();

        return Ok(allThingsDone);
    }
}
  1. Implementation

Composing

This article covers the topic of composing in Umbraco.

PreviousData Persistence (CRUD)NextIntegration Testing
  • Overview
  • Example - Creating a Composer to listen for ContentSavingNotification
  • Example - Explicitly Registering a new custom OEmbedProvider
  • ComponentComposer
  • Collections
  • Types of Collections
  • Example - Modifying Collections
  • Attributes
  • ComposeBefore and ComposeAfter
  • Weight
  • HideFromTypeFinder
  • DisableComposer & Disable
  • Runtime Levels
  • Example of using Ordered Collections and adding types explicitly
  • Example of using Lazy Collections with Type Scanning