Skip to content

Commit 1f1d0c3

Browse files
authored
Merge pull request #7223 from umbraco/IPublishedContentQuery
Added updates from v15 to v16
2 parents 57a68f5 + f329145 commit 1f1d0c3

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

16/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:

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ public class SearchService
2828

2929
Now you can access the `IPublishedContentQuery` through `_publishedContentQuery`
3030

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+
```
46+
3147
## Examples
3248

3349
### .Search(string term)

0 commit comments

Comments
 (0)