Skip to content

Add info about IPublishedContentQuery in background task #6997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions 15/umbraco-cms/implementation/services/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ public class HandleUnPublishingHandler : INotificationHandler<ContentUnpublished
}
```

#### Accessing the Published Content Cache via IPublishedContentQuery

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.

#### 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:
Expand Down
20 changes: 18 additions & 2 deletions 15/umbraco-cms/reference/querying/ipublishedcontentquery.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<IPublishedContentQuery>();
```

## Examples

Expand Down