U4-9226 - Allow for querying published content cache outside of a Web request

Created by Shannon Deminick 28 Nov 2016, 12:31:59 Updated by Shannon Deminick 01 Nov 2017, 05:16:48

Tags: Question

The reason why we should do this is because currently you can only query published content from the UmbracoContext which is a Request based item and in some cases people want to query the content cache from a background thread. People have then reverted to using an EnsureContext to fake an UmbracoContext just to create an UmbracoHelper but in many cases this isn't done safely.

Instead we should allow people to query published content at the application level or provide a safe way to create an UmbracoContext or another lightweight context on a background thread - and document it! Essentially we only need to expose ITypedPublishedContentQuery


Ryan Lewis 09 Feb 2017, 12:22:25

Would find this very useful. Current use case I have is a GatheringNodeData event handler needing to use ContentService in order to query a parent document to obtain some indexable data.

Shannon Deminick 09 Feb 2017, 23:40:06

You don't need published content to use the ContentService - the ContentService (along with all other Umbraco.Core.Services.*) deal with database data only, nothing to do with published content. You shouldn't rely on published content for information going into your indexes anyways and there is no way that you could really know that the published content is up to date when your indexes are being updated because both of these happen at the same time - both of these are considered front-end caches.

Ryan Lewis 10 Feb 2017, 09:58:36

Sorry that I wasn't clear - currently using ContentService, as the published cache is unavailable. Your other point makes sense though.

Alex Lindgren 22 Feb 2017, 19:00:20

@Ryan.Lewis I'm not sure if this is a safe way, but I blogged about adding the helper as an extra parameter to the GatheringNodeData event handler at http://www.alexlindgren.com/archive/using-umbracohelper-from-event-handlers/

Again, I'm not sure this is 'safe' as the UmbracoContext object does not seem to be fully initialized but I was able to use it to get IPublishedContent (helper.TypedContent(e.NodeId); - would love to know what @Shandem and others thinks about think about this. Also, I also found a way to 'fake' an UmbracoContext at https://gist.github.com/sniffdk/7600822.

I think it would be better to be able to access the published cache without doing this, but I thought I'd share these techniques.

Priority: Normal

Type: Feature (request)

State: Open


Difficulty: Normal


Backwards Compatible: True

Fix Submitted:

Affected versions:

Due in version:


Story Points: