Orchardcore content queries

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore content queries is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for querying content items in Orchard Core using YesSql and saved queries. Covers ContentItemIndex queries and its indexes (including IDX_ContentItemIndex_ContentItemId), custom YesSql indexes, ISession and IContentManager queries, saved SQL queries of the Queries module (Cyqwel parser, SELECT

How to use this skill

Skill file

Orchard Core Content Queries - Prompt Templates

Query Content Items

You are an Orchard Core expert. Generate code for querying content items using YesSql indexes and IContentManager.

Guidelines

Choosing the Right Mechanism

"Query" and "index" mean three different things in Orchard Core. Pick the one that matches the goal.

| Goal | Mechanism | Skill |

|------|-----------|-------|

| Fast relational lookup from C# by a column | YesSql MapIndex table + ISession.Query<T, TIndex>() | this skill, orchardcore-yessql-indexes |

| Named, stored query run from Liquid, Razor, GraphQL or api/queries | Queries module (IQueryManager, Sql, Lucene, Elasticsearch sources) | this skill |

| Full-text search over content | Index profiles fed by index handlers | orchardcore-indexing, orchardcore-search-indexing |

Querying with ContentItemIndex

using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Records;
using YesSql;

public sealed class ContentQueryService
{
    private readonly ISession _session;

    public ContentQueryService(ISession session)
    {
        _session = session;
    }

    // Query by content type
    public async Task<IEnumerable<ContentItem>> GetByTypeAsync(string contentType)
    {
        return await _session
            .Query<ContentItem, ContentItemIndex>(x => x.ContentType == contentType && x.Published)
            .ListAsync();
    }

    // Query by content type with paging
    public async Task<IEnumerable<ContentItem>> GetPagedAsync(string contentType, int page, int pageSize)
    {
        return await _session
            .Query<ContentItem, ContentItemIndex>(x => x.ContentType == contentType && x.Published)
            .OrderByDescending(x => x.CreatedUtc)
            .Skip((page - 1) * pageSize)
            .Take(pageSize)
            .ListAsync();
    }

    // Query by display text
    public async Task<ContentItem> GetByDisplayTextAsync(string displayText)
    {
        return await _session
            .Query<ContentItem, ContentItemIndex>(x => x.DisplayText == displayText && x.Published)
            .FirstOrDefaultAsync();
    }

    // Count content items
    public async Task<int> CountByTypeAsync(string contentType)
    {
        return await _session
            .Query<ContentItem, ContentItemIndex>(x => x.ContentType == contentType && x.Published)
            .CountAsync();
    }

    // Query latest versions (including drafts)
    public async Task<IEnumerable<ContentItem>> GetLatestAsync(string contentType)
    {
        return await _session
            .Query<ContentItem, ContentItemIndex>(x => x.ContentType == contentType && x.Latest)
            .ListAsync();
    }
}

ContentItemIndex Fields Reference

The built-in ContentItemIndex has these fields:

In Orchard Core 4.0 the SQL indexes of ContentItemIndex are IDX_FK_ContentItemIndex (created by YesSql on DocumentId), IDX_ContentItemIndex_DocumentId_ContentType (DocumentId, ContentType, CreatedUtc, ModifiedUtc, PublishedUtc, Published, Latest), IDX_ContentItemIndex_ContentItemId (ContentItemId, DocumentId, Published, Latest), IDX_ContentItemIndex_ContentType (ContentType, DocumentId, Published, Latest), IDX_ContentItemIndex_Owner (Owner, DocumentId, Published, Latest) and IDX_ContentItemIndex_ContentItemVersionId (ContentItemVersionId, DocumentId), plus IDX_ContentItemIndex_ModifiedUtc (ModifiedUtc, DocumentId, Latest, Published, ContentType) and IDX_ContentItemIndex_ContentType_ModifiedUtc (ContentType, ModifiedUtc, DocumentId, Latest, Published) that serve the content items admin list order. Filters on one content type (admin list and its count, GraphQL, pickers, sitemaps), on the owner, on a content item id, or on a version id (GetVersionAsync, loading Lucene or Elasticsearch results) now seek instead of scanning. The 3.x indexes IDX_ContentItemIndex_DocumentId, IDX_ContentItemIndex_DocumentId_Owner, IDX_ContentItemIndex_DocumentId_Author and IDX_ContentItemIndex_DocumentId_DisplayText are dropped. To filter on another column, such as Author or DisplayText, add your own index from a data migration with SchemaBuilder.AlterIndexTableAsync<ContentItemIndex>(). Existing tenants get these changes through migration steps (versions 8 and 9). On a large table they take a moment; with YesSql 6.3 they no longer block writes on SQL Server editions with online index operations and on MySQL, but still do on PostgreSQL and SQLite. Prefer filtering on ContentItemId, ContentType or Owner together with Published or Latest.

Creating a Custom YesSql Index

using OrchardCore.ContentManagement;
using YesSql.Indexes;

public sealed class {{PartName}}Index : MapIndex
{
    public string ContentItemId { get; set; }
    public string {{PropertyName}} { get; set; }
    public bool Published { get; set; }
}

public sealed class {{PartName}}IndexProvider : IndexProvider<ContentItem>
{
    public override void Describe(DescribeContext<ContentItem> context)
    {
        // A content item has one {{PartName}}, so the index holds at most one record per version.
        context.For<{{PartName}}Index>()
            .ExclusiveToDocumentType()
            .SingleRecordPerDocument()
            .When(contentItem => contentItem.Has<{{PartName}}>())
            .Map(contentItem =>
            {
                if (!contentItem.TryGet<{{PartName}}>(out var part))
                {
                    return null;
                }

                return new {{PartName}}Index
                {
                    ContentItemId = contentItem.ContentItemId,
                    {{PropertyName}} = part.{{PropertyName}},
                    Published = contentItem.Published
                };
            });
    }
}

Registering Custom Index and Migration

using OrchardCore.Data.Migration;
using YesSql.Sql;

public sealed class Migrations : DataMigration
{
    public async Task<int> CreateAsync()
    {
        await SchemaBuilder.CreateMapIndexTableAsync<{{PartName}}Index>(table => table
            .Column<string>("ContentItemId", col => col.WithLength(26))
            .Column<string>("{{PropertyName}}", col => col.WithLength(256))
            .Column<bool>("Published")
        );

        await SchemaBuilder.AlterIndexTableAsync<{{PartName}}Index>(table => table
            .CreateIndex("IDX_{{PartName}}Index_{{PropertyName}}",
                "{{PropertyName}}",
                "Published"));

        return 1;
    }
}

Registering Index Provider in Startup

using OrchardCore.Data.Migration;
using YesSql.Indexes;

public sealed class Startup : StartupBase
{
    public override void ConfigureServices(IServiceCollection services)
    {
        services.AddIndexProvider<{{PartName}}IndexProvider>();
        services.AddDataMigration<Migrations>();
    }
}

Querying with Custom Index

public sealed class {{PartName}}QueryService
{
    private readonly ISession _session;

    public {{PartName}}QueryService(ISession session)
    {
        _session = session;
    }

    public async Task<IEnumerable<ContentItem>> GetByPropertyAsync(string value)
    {
        return await _session
            .Query<ContentItem, {{PartName}}Index>(x => x.{{PropertyName}} == value && x.Published)
            .ListAsync();
    }
}

Using IContentManager

using OrchardCore.ContentManagement;

public sealed class ContentService
{
    private readonly IContentManager _contentManager;

    public ContentService(IContentManager contentManager)
    {
        _contentManager = contentManager;
    }

    // Get by ID
    public async Task<ContentItem> GetByIdAsync(string contentItemId)
    {
        return await _contentManager.GetAsync(contentItemId);
    }

    // Get specific version
    public async Task<ContentItem> GetVersionAsync(string versionId)
    {
        return await _contentManager.GetVersionAsync(versionId);
    }

    // Create new content item
    public async Task<ContentItem> CreateAsync(string contentType)
    {
        var contentItem = await _contentManager.NewAsync(contentType);
        await _contentManager.CreateAsync(contentItem, VersionOptions.Draft);
        return contentItem;
    }

    // Publish a content item
    public async Task PublishAsync(ContentItem contentItem)
    {
        await _contentManager.PublishAsync(contentItem);
    }

    // Update content item
    public async Task UpdateAsync(ContentItem contentItem)
    {
        await _contentManager.UpdateAsync(contentItem);
    }

    // Remove content item
    public async Task RemoveAsync(ContentItem contentItem)
    {
        await _contentManager.RemoveAsync(contentItem);
    }
}

Combining Predicates and Ordering

using YesSql;

public sealed class AdvancedQueryService
{
    private readonly ISession _session;

    public AdvancedQueryService(ISession session)
    {
        _session = session;
    }

    public async Task<IEnumerable<ContentItem>> QueryWithSqlAsync()
    {
        return await _session
            .Query<ContentItem, ContentItemIndex>()
            .Where(x => x.Published && x.ContentType == "Article")
            .OrderByDescending(x => x.PublishedUtc)
            .Take(10)
            .ListAsync();
    }
}

Saved SQL Queries (Queries Module)

The SQL Queries feature (OrchardCore.Queries.Sql, depends on OrchardCore.Queries) stores named SQL queries that run against the index tables. They are used from Liquid, Razor, scripting, GraphQL, the api/queries/{name} endpoint, data sources of data pipelines (see orchardcore-data-sources), and widgets.

SQL Syntax and Validation

select DocumentId
from ContentItemIndex
where Published = true and ContentType = @contenttype:'BlogPost'
order by CreatedUtc desc
limit @limit:10

Parameters and Templates

{% if limit > 0 %}
    select DocumentId from ContentItemIndex where Published = true limit @limit
{% else %}
    select DocumentId from ContentItemIndex where Published = true
{% endif %}

Recipe Step

{
  "steps": [
    {
      "name": "Feature",
      "enable": [ "OrchardCore.Queries", "OrchardCore.Queries.Sql" ]
    },
    {
      "name": "Queries",
      "Queries": [
        {
          "Source": "Sql",
          "Name": "RecentBlogPosts",
          "Template": "select DocumentId from ContentItemIndex where ContentType = 'BlogPost' and Published = true order by CreatedUtc desc limit @limit:3",
          "Schema": "{ \"type\": \"ContentItem/BlogPost\" }",
          "ReturnContentItems": true
        }
      ]
    }
  ]
}

Using Saved Queries

{% assign posts = Queries.RecentBlogPosts | query: limit: 5 %}
{% for item in posts %}
    {{ item | display_text }}
{% endfor %}
@foreach (var contentItem in await Orchard.ContentQueryAsync("RecentBlogPosts"))
{
    @await Orchard.DisplayAsync(contentItem)
}

The executeQuery(name, parameters) scripting function runs a query from JavaScript (workflows, recipes).

Web API

api/queries/{name} runs a query with GET or POST; parameters is a JSON object, passed in the query string or as the POST body. The response holds items and count. The caller needs ExecuteApi_{QueryName} (implied by ExecuteApiAll and ManageQueries); a query the caller may not execute returns 404.

POST /api/queries/RecentBlogPosts
Content-Type: application/json

{ "limit": 5 }

In Orchard Core 4.0, the endpoint is split into two actions with endpoint names ApiExecuteQueryGet and ApiExecuteQueryPost (route, verbs and parameters unchanged), so OpenAPI documents are valid (see orchardcore-openapi).

Permissions

| Permission | Member |

|------------|--------|

| Manage SQL Queries (security critical) | OrchardCore.Queries.QueriesPermissions.ManageSqlQueries |

| Manage queries | OrchardCore.Queries.QueryPermissions.ManageQueries |

| Execute Api - All queries | OrchardCore.Queries.QueryPermissions.ExecuteApiAll |

| Execute Api - {name} | QueryPermissions.CreatePermissionForQuery(name) |

Since Orchard Core 4.0, the GraphQL fields that expose named SQL, Lucene and Elasticsearch queries also require ExecuteApi_{QueryName}, like the REST endpoint.

Running Queries from Code

IQueryManager (namespace OrchardCore.Queries) creates, stores and runs named queries. ExecuteQueryAsync resolves the IQuerySource keyed by Query.Source.

using System.Text.Json.Nodes;
using OrchardCore.ContentManagement;
using OrchardCore.Queries;

public sealed class RecentPostsService
{
    private readonly IQueryManager _queryManager;

    public RecentPostsService(IQueryManager queryManager)
    {
        _queryManager = queryManager;
    }

    public async Task EnsureQueryAsync()
    {
        if (await _queryManager.GetQueryAsync("RecentBlogPosts") is not null)
        {
            return;
        }

        // The data keys match the recipe step: Name, Template, Schema, ReturnContentItems.
        var query = await _queryManager.NewAsync("Sql", new JsonObject
        {
            ["Name"] = "RecentBlogPosts",
            ["Template"] = "select DocumentId from ContentItemIndex where ContentType = 'BlogPost' and Published = true order by CreatedUtc desc limit @limit:10",
            ["ReturnContentItems"] = true,
        });

        await _queryManager.SaveAsync(query);
    }

    public async Task<IEnumerable<ContentItem>> GetRecentAsync(int limit)
    {
        var query = await _queryManager.GetQueryAsync("RecentBlogPosts");

        var results = await _queryManager.ExecuteQueryAsync(query, new Dictionary<string, object>
        {
            ["limit"] = limit,
        });

        return results.Items.OfType<ContentItem>();
    }
}

| Member | Purpose |

|--------|---------|

| NewAsync(source, data) | Build a query for a registered source; returns null when the source is unknown. |

| UpdateAsync(query, data) / SaveAsync(params Query[]) | Persist changes. |

| GetQueryAsync(name) / DeleteQueryAsync(params string[]) | Load or remove by name. |

| ListQueriesAsync(QueryContext) / PageQueriesAsync(page, pageSize, context) | List queries; the ListQueriesBySourceAsync(source) extension filters by source. |

| ExecuteQueryAsync(query, parameters) | Run the query and return IQueryResults.Items. |

How a SQL query executes:

  1. The Template (stored in SqlQueryMetadata) is rendered as Liquid with the parameters as Liquid values.
  2. The result is parsed by Cyqwel and translated to the tenant dialect, schema and table prefix. A parse failure is logged as an error and returns an empty result instead of throwing.
  3. The SQL runs through Dapper with the parameters bound as SQL parameters.
  4. With ReturnContentItems the DocumentId column (or the first long column) is used to load ContentItem documents; otherwise each row is returned as a JsonObject.

Custom Query Sources

Implement IQuerySource and register it with AddQuerySource<T>(name), which registers the source as a keyed service under its name. Pair it with a DisplayDriver<Query> for the admin editor and an IQueryHandler (or QueryHandlerBase) that copies source-specific data from the recipe or editor JSON into the query.

using System.Text.Json.Nodes;
using OrchardCore.Entities;
using OrchardCore.Queries;
using OrchardCore.Queries.Core;

public sealed class ProductQueryMetadata
{
    public string Filter { get; set; }
}

public sealed class ProductQueryResults : IQueryResults
{
    public IEnumerable<object> Items { get; set; } = [];
}

public sealed class ProductQuerySource : IQuerySource
{
    public const string SourceName = "Products";

    public string Name => SourceName;

    public Task<IQueryResults> ExecuteQueryAsync(Query query, IDictionary<string, object> parameters)
    {
        var metadata = query.GetOrCreate<ProductQueryMetadata>();

        // Run the query against your store using metadata.Filter and the parameters.
        IQueryResults results = new ProductQueryResults();

        return Task.FromResult(results);
    }
}

public sealed class ProductQueryHandler : QueryHandlerBase
{
    public override Task InitializingAsync(InitializingQueryContext context)
        => CopyAsync(context);

    public override Task UpdatingAsync(UpdatingQueryContext context)
        => CopyAsync(context);

    private static Task CopyAsync(DataQueryContextBase context)
    {
        if (context.Query.Source != ProductQuerySource.SourceName)
        {
            return Task.CompletedTask;
        }

        var filter = context.Data?[nameof(ProductQueryMetadata.Filter)]?.GetValue<string>();

        if (!string.IsNullOrEmpty(filter))
        {
            context.Query.Put(new ProductQueryMetadata { Filter = filter });
        }

        return Task.CompletedTask;
    }
}
[RequireFeatures("OrchardCore.Queries")]
public sealed class Startup : StartupBase
{
    public override void ConfigureServices(IServiceCollection services)
    {
        services.AddQuerySource<ProductQuerySource>(ProductQuerySource.SourceName);
        services.AddScoped<IQueryHandler, ProductQueryHandler>();
        // Your DisplayDriver<Query> that renders the Filter editor.
        services.AddDisplayDriver<Query, ProductQueryDisplayDriver>();
    }
}

IQueryHandler exposes InitializingAsync, InitializedAsync, LoadedAsync, UpdatingAsync, UpdatedAsync, DeletingAsync and DeletedAsync. Every handler sees every query, so check context.Query.Source first.

Upgrading from 3.x

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.