Skip to content

Commit 57a68f5

Browse files
authored
Merge pull request #6997 from bjarnef/patch-29
Add info about `IPublishedContentQuery` in background task
2 parents f148275 + 1cc4067 commit 57a68f5

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

15/umbraco-cms/implementation/services/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ public class HandleUnPublishingHandler : INotificationHandler<ContentUnpublished
223223
}
224224
```
225225

226+
#### Accessing the Published Content Cache via IPublishedContentQuery
227+
228+
When fetching multiple content items by ID, using `UmbracoContext.Content` is limited because it only allows retrieving one item at a time. To query multiple items efficiently, you can use `IPublishedContentQuery`. For more details, see the [IPublishedContentQuery](../../reference/querying/ipublishedcontentquery.md) article.
229+
226230
#### Accessing the Published Content Cache from a Content Finder / UrlProvider
227231

228232
Inside a ContentFinder access to the content cache is possible by injecting `IUmbracoContextAccessor` into the constructor and provided via the PublishedRequest object:

15/umbraco-cms/reference/querying/ipublishedcontentquery.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The `IPublishedContentQuery` interface contains different query methods for acce
88

99
## How to inject IPublishedContentQuery
1010

11-
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.
11+
To inject the `IPublishedContentQuery` into your services, you must add a using statement for `Umbraco.Cms.Core` and inject the service using the constructor.
1212

1313
```csharp
1414
using Umbraco.Cms.Core;
@@ -26,7 +26,23 @@ public class SearchService
2626
}
2727
```
2828

29-
Now you can access the `IPublishedContentQuery` through `_publishedContentQuery`
29+
Now you can access the `IPublishedContentQuery` through `_publishedContentQuery`.
30+
31+
## Accessing the Published Content Cache via `IPublishedContentQuery`
32+
33+
Sometimes, you may need to fetch multiple content items by ID, but `UmbracoContext.Content` only allows fetching a single content item at a time.
34+
35+
{% hint style="warning" %}
36+
In a background task, accessing the content cache using an injected `IPublishedContentQuery` or `IPublishedContentQueryAccessor` will not work, as they rely on `HttpContext`.
37+
{% endhint %}
38+
39+
Instead, use the following approach:
40+
41+
```csharp
42+
using UmbracoContextReference _ = _umbracoContextFactory.EnsureUmbracoContext();
43+
using IServiceScope serviceScope = _serviceProvider.CreateScope();
44+
IPublishedContentQuery query = serviceScope.ServiceProvider.GetRequiredService<IPublishedContentQuery>();
45+
```
3046

3147
## Examples
3248

0 commit comments

Comments
 (0)