In this tutorial, you will learn about the differences between UmbracoHelper and the ContentTypeService while querying for data using Umbraco CMS. Umbraco ships with several useful APIs and one common misconception is when to use the UmbracoHelper, compared to the ContentTypeService. If you look at the constructor of the UmbracoHelper, you will notice that it takes a parameter type of UmbracoContext. This is one of the first big differences.

The UmbracoContext is a wrapper around the normal .NET HTTPContext. The main difference is that it also exposes some useful Umbraco functionality. Like HTTPContext the UmbracoContext works on a per-thread basis. This means the scope of UmbracoContext.Current is restricted per web request. If you need to access data within Umbraco outside of a web request you can not use this approach. For example, you need to access something in Umbraco in a scheduled task, or an HTTP module. Trying to access UmbracoHelper when the HttpContext is not set, can result in unexpected side effects. When you need to access Umbraco data outside of a web request, an alternative way to query the CMS is use the ContentTypeService, like so:

There are downsides to using ApplicationContext. First, the ApplicationContext doesn't use the Umbraco cache and will request everything from the database. Calling a database for each operation is less performant. As long as your request is run infrequently, or on startup, in 98% of the cases though, I don't think the average Joe would notice anything.

We have two major differences in the UmbracoHelper Vs ContentTypeService considerations. First, UmbracoHelper uses the frontend Umbraco cache so it is quick. UmbracoHelperis good when you need to access Umbraco data in read-only mode. The UmbracoHelper needs the HttpContext fully initialized to work as expected. This means that calling the UmbracoHelper in code that is run before the request pipeline is started, or, outside of a normal web request, can result in errors. The final difference is that UmbracoHelpercan only be used for read-only commands. If you want to update Umbraco data use ContentTypeService.

I really want to use UmbracoHelper

If you desperately want to use UmbracoHelper and you are happy to take some risks, you can create a UmbracoContext outside of a normal web request and new up a new UmbracoHelper object. First, you could simply try to set the context yourself, like so (NOTE: make sure you don't get confused with the defunct Umbraco.Presentation.UmbracoContext):

You could consider manually creating the UmbracoContext. The code to do this is shown below. However, I do not recommend this approach.

If you want to access Umbraco data and you do not want to use ContentTypeService and you do not want to manually new up UmbracoHelper, my next bit of advice is to re-think your architecture. Look at what you are doing and then try to do it in a different way. In 99% of instances, if you are trying to do something the CMS doesn't allow you to do easily, you are likely trying to do something silly. Stop, take a breath and re-think. Better to re-write some code now than to deliver some janky software later 🤪. Happy Coding 🤘