> For the complete documentation index, see [llms.txt](https://docs.umbraco.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.umbraco.com/umbraco-cms/19.latest/develop-with-umbraco/templating-and-rendering/querying/ipublishedcontentquery.md).

# IPublishedContentQuery

Querying content with IPublishedContentQuery in Umbraco

The `IPublishedContentQuery` interface contains methods for querying strongly typed content.

{% hint style="warning" %}
**Use with caution**

The `IPublishedContentQuery` has limited filtering capabilities. It should only be used for broad, site-wide search functionality where filtering is not important.

Do *not* combine `IPublishedContentQuery` with post-filtering of search results in memory. This will severely degrade your site performance. Instead, use the [search feature](https://docs.umbraco.com/umbraco-cms/19.latest/develop-with-umbraco/search.md) for advanced querying.
{% endhint %}

## 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.

```csharp
using Umbraco.Cms.Core;

namespace Umbraco.Docs.Samples.Web.Services;

public class SearchService
{
    private readonly IPublishedContentQuery _publishedContentQuery;

    public SearchService(IPublishedContentQuery publishedContentQuery)
    {
        _publishedContentQuery = publishedContentQuery;
    }
}
```

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

## Examples

### .Search(string term)

By default, `IPublishedContentQuery` will search on Umbraco's 'public' search index for any published content matching the provided search term.

```csharp
public IEnumerable<PublishedSearchResult> Search(string searchTerm)
{
    foreach (var result in _publishedContentQuery.Search(searchTerm))
    {
        yield return result;
    }
}
```

### .Search(string term, int skip, int take, out long totalRecords)

Specifying the number of records 'to skip' and the number of records 'to take' improves performance with many matching search results. This approach is beneficial when there is a requirement to implement paging.

```csharp
public IEnumerable<PublishedSearchResult> Search(string searchTerm, int skip = 5, int take = 10)
{
    foreach (var result in _publishedContentQuery.Search(searchTerm, skip, take, out _))
    {
        yield return result;
    }
}
```

## Limitations for background tasks

The `IPublishedContentQuery` cannot be injected into a background task, as it relies on context. Instead, create a service scope and resolve `IPublishedContentQuery` from it:

```csharp
public class MyBackgroundJob : RecurringBackgroundJobBase
{
    private readonly IServiceScopeFactory _serviceScopeFactory;
    private readonly IUmbracoContextFactory _umbracoContextFactory;

    public MyBackgroundJob(
        IServiceScopeFactory serviceScopeFactory,
        IUmbracoContextFactory umbracoContextFactory)
        : base(TimeSpan.FromMinutes(5))
    {
        _serviceScopeFactory = serviceScopeFactory;
        _umbracoContextFactory = umbracoContextFactory;
    }

    public override Task RunJobAsync(CancellationToken cancellationToken)
    {
        using UmbracoContextReference _ = _umbracoContextFactory.EnsureUmbracoContext();
        using IServiceScope serviceScope = _serviceScopeFactory.CreateScope();
        IPublishedContentQuery publishedContentQuery = serviceScope.ServiceProvider.GetRequiredService<IPublishedContentQuery>();
        IEnumerable<PublishedSearchResult> results = publishedContentQuery.Search("[your search term]");

        // do something with the results here
        
        return Task.CompletedTask;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.umbraco.com/umbraco-cms/19.latest/develop-with-umbraco/templating-and-rendering/querying/ipublishedcontentquery.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
