Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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

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

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