From 64eecb5d13e563e4109cf60cd29a7313e0c50dba Mon Sep 17 00:00:00 2001 From: Bjarne Fyrstenborg Date: Wed, 2 Apr 2025 12:48:49 +0200 Subject: [PATCH 1/6] Add info about IPublishedContentQuery in background task --- 15/umbraco-cms/implementation/services/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/15/umbraco-cms/implementation/services/README.md b/15/umbraco-cms/implementation/services/README.md index 1b2497ae0d1..e3a00374737 100644 --- a/15/umbraco-cms/implementation/services/README.md +++ b/15/umbraco-cms/implementation/services/README.md @@ -223,6 +223,19 @@ public class HandleUnPublishingHandler : INotificationHandler(); +``` + #### Accessing the Published Content Cache from a Content Finder / UrlProvider Inside a ContentFinder access to the content cache is possible by injecting `IUmbracoContextAccessor` into the constructor and provided via the PublishedRequest object: From 306a8c640e269a1893682153bf5d15013c45793e Mon Sep 17 00:00:00 2001 From: Bjarne Fyrstenborg Date: Thu, 3 Apr 2025 15:42:12 +0200 Subject: [PATCH 2/6] Update 15/umbraco-cms/implementation/services/README.md Co-authored-by: Esha Noronha <82437098+eshanrnh@users.noreply.github.com> --- 15/umbraco-cms/implementation/services/README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/15/umbraco-cms/implementation/services/README.md b/15/umbraco-cms/implementation/services/README.md index e3a00374737..d5099cc2b17 100644 --- a/15/umbraco-cms/implementation/services/README.md +++ b/15/umbraco-cms/implementation/services/README.md @@ -224,12 +224,14 @@ public class HandleUnPublishingHandler : INotificationHandler Date: Mon, 7 Jul 2025 10:16:23 +0200 Subject: [PATCH 3/6] Added link to Ipublishedcontentquery article --- 15/umbraco-cms/implementation/services/README.md | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/15/umbraco-cms/implementation/services/README.md b/15/umbraco-cms/implementation/services/README.md index d5099cc2b17..268dc0ef467 100644 --- a/15/umbraco-cms/implementation/services/README.md +++ b/15/umbraco-cms/implementation/services/README.md @@ -224,19 +224,8 @@ public class HandleUnPublishingHandler : INotificationHandler(); -``` +Sometimes, you may need to fetch multiple content items by ID, but `UmbracoContext.Content` only allows fetching a single content item at a time. For more information, see the [IPublishedContentQuery](../../reference/querying/ipublishedcontentquery.md) article. #### Accessing the Published Content Cache from a Content Finder / UrlProvider From e87166d077ac1b3e2542bd7d834ab010be2e18b3 Mon Sep 17 00:00:00 2001 From: Esha Noronha <82437098+eshanrnh@users.noreply.github.com> Date: Mon, 7 Jul 2025 10:18:54 +0200 Subject: [PATCH 4/6] Added Accessing the Published Content Cache via `IPublishedContentQuery` section --- .../querying/ipublishedcontentquery.md | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/15/umbraco-cms/reference/querying/ipublishedcontentquery.md b/15/umbraco-cms/reference/querying/ipublishedcontentquery.md index d8acd00a8a9..de8197c45c7 100644 --- a/15/umbraco-cms/reference/querying/ipublishedcontentquery.md +++ b/15/umbraco-cms/reference/querying/ipublishedcontentquery.md @@ -8,7 +8,7 @@ The `IPublishedContentQuery` interface contains different query methods for acce ## How to inject IPublishedContentQuery -In order to inject the `IPublishedContentQuery` into your services, you must add a using statement for `Umbraco.Cms.Core` and inject the service using the constructor. +To inject the `IPublishedContentQuery` into your services, you must add a using statement for `Umbraco.Cms.Core` and inject the service using the constructor. ```csharp using Umbraco.Cms.Core; @@ -26,7 +26,23 @@ public class SearchService } ``` -Now you can access the `IPublishedContentQuery` through `_publishedContentQuery` +Now you can access the `IPublishedContentQuery` through `_publishedContentQuery`. + +## Accessing the Published Content Cache via `IPublishedContentQuery` + +Sometimes, you may need to fetch multiple content items by ID, but `UmbracoContext.Content` only allows fetching a single content item at a time. + +{% hint style="warning" %} +In a background task, accessing the content cache using an injected `IPublishedContentQuery` or `IPublishedContentQueryAccessor` will not work, as they rely on `HttpContext`. +{% endhint %} + +Instead, use the following approach: + +```csharp +using UmbracoContextReference _ = _umbracoContextFactory.EnsureUmbracoContext(); +using IServiceScope serviceScope = _serviceProvider.CreateScope(); +IPublishedContentQuery query = serviceScope.ServiceProvider.GetRequiredService(); +``` ## Examples From cfb22c874c8bec48654cb7692cd19eaf8bfe62ab Mon Sep 17 00:00:00 2001 From: Esha Noronha <82437098+eshanrnh@users.noreply.github.com> Date: Mon, 7 Jul 2025 10:20:10 +0200 Subject: [PATCH 5/6] Update 15/umbraco-cms/implementation/services/README.md --- 15/umbraco-cms/implementation/services/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/15/umbraco-cms/implementation/services/README.md b/15/umbraco-cms/implementation/services/README.md index 268dc0ef467..ca39e40f7dd 100644 --- a/15/umbraco-cms/implementation/services/README.md +++ b/15/umbraco-cms/implementation/services/README.md @@ -225,7 +225,7 @@ public class HandleUnPublishingHandler : INotificationHandler Date: Mon, 7 Jul 2025 10:25:58 +0200 Subject: [PATCH 6/6] Update 15/umbraco-cms/implementation/services/README.md --- 15/umbraco-cms/implementation/services/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/15/umbraco-cms/implementation/services/README.md b/15/umbraco-cms/implementation/services/README.md index ca39e40f7dd..f9456976c02 100644 --- a/15/umbraco-cms/implementation/services/README.md +++ b/15/umbraco-cms/implementation/services/README.md @@ -225,7 +225,7 @@ public class HandleUnPublishingHandler : INotificationHandler