Getting domains list for node programmatically in Umbraco CMS

Once you define hostnames for a Content node in Umbraco CMS in the back office –  it’s easy to get the list in C# within a few lines of code.

Cultures And Hostnames For Umbraco Node
Cultures and hostnames for Umbraco node

To do so you should use the IDomainService and grab the instance.

private readonly IDomainService _domainService;

You can get service instance using static service locator as follows:

_domainService = Current.Factory.GetInstance<IDomainService>();

The IDomainService has a very easy-to-use method named GetAssignedDomains that returns a list of domains for a given node id.

var domains = _domainService.GetAssignedDomains(1501, includeWildcards: true);

Example output:

Domains List For Umbraco Node
Domains list for Umbraco node
Domains List For Umbraco Node Immediate Window
Domains list for Umbraco node – immediate window

Similar Posts