Can I create a JCR query to retrieve list elements in manual order?
Question
Is it possible to create a JCR query to retrieve the elements of a list sorted manually (according to the order defined by drag-and-drop in Jahia’s Content Editor)?
Answer
Answer is: No.
A JCR query (SQL-2 or XPath) always returns an aggregation of nodes that match the query conditions, regardless of the parent list they belong to.
Because results are aggregated across the repository:
- The manual child order defined under each parent is lost.
- The query engine can only order results by explicit properties (customOrder, jcr:created, etc.) or pseudo-fields like jcr:path
- There is no way to “ORDER BY manual order” in a query.
This means that even if two nodes each have a position “2” in their own lists, a global query will not know how to reconcile that — it just returns one flat, aggregated result set.
Workarounds
1. When you iterate over the children of a given parent via the JCR API, the iteration respects the manual child order.
Node parent = session.getNode("/sites/acme/home/myList");
for (NodeIterator it = parent.getNodes(); it.hasNext();) {
Node child = it.nextNode(); // Manual order preserved
}
2. Jahia’s GraphQL API returns children in their manual order (drag-and-drop defined in Content Editor) when using the children field:
query {
jcr {
nodeByPath(path: "/sites/acme/home/myList") {
children(
typesFilter: { types: ["mynt:item"] }
) {
nodes {
uuid
displayName
}
}
}
}
}
This returns only the children of one list, sorted as defined manually.
3. Introduce an explicit “order” property
If you must rely on JCR queries (e.g., SQL-2), add and maintain a numeric property (e.g., customOrder) on each child. You can then sort by it:
SELECT * FROM [mynt:item] AS i
WHERE ISCHILDNODE(i, '/sites/acme/home/myList')
ORDER BY i.[customOrder] ASC