Hello,
I’m trying to create a query that will list all children of xwiki:A.B, but only created from date1 to date2.
The query to display each separately works, but not combined:
#set($reference = $services.model.createDocumentReference(‘xwiki’, ‘A’, ‘B’))
#set($MyTopSpaceName = $xwiki.getDocument($reference))
#set($MyTopSpace=“‘${MyTopSpaceName}’”)
#foreach($item in $services.query.xwql(“select space.name from XWikiSpace as space where space.parent = $MyTopSpace and doc.creationDate > :date1 and doc.creationDate < :date2”).bindValue(“date1”, $datetool.toDate(‘yyyy-MM-dd’, ‘2023-01-01’)).bindValue(“date2”, $datetool.toDate(‘yyyy-MM-dd’, ‘2025-01-01’)).execute())
bla bla bla
I would welcome your assistance.
Thank you.
Hi @georgiys,
Getting children of a space is also known as getting sub-pages of a page. You can look at these example https://extensions.xwiki.org/xwiki/bin/view/Extension/Query%20Module#HFindingpagesundernestedpages and, excepting the date comparison, it would be something as simple as:
$services.query.xwql('where doc.space like :space').bindValue('space', "${mySpace}%" ).execute()
of, if you want to avoid documents from spaces like MainS
and only get them from Main
$services.query.xwql('where doc.fullName like :space').bindValue('space', "${mySpace}.%").execute()
Hope it helps,
Alex
Hi Alex,
Thank you. I forgot to mention that I’m looking only for direct children of xwiki.A.B, i.e. pages whose parent is xwiki.A.B
Your code now gives me many more results, including xwiki.A.B.Tasks.Task_1.WebHome, etc.
Is there an easy way to filter those out? I.e. get only xwiki.A.B.page1.WebHome, xwiki.A.B.page2.WebHome, etc.
Your pages are non-terminal pages and none of them are direct children of your page (xwiki.A.B).
xwiki.A.B.page1.WebHome
is not direct children
xwiki.A.B.page1
is direct children
It looks like you are aiming to get xwiki.A.B.page1.WebHome
and xwiki.A.B.page2.WebHome
but you will also get xwiki.A.B.Tasks.WebHome
.
However, in order to get only these 3 direct children, you will need an additional check, outside the query. The following should provide the expected results:
{{velocity}}
#set ($spaceName = 'Main.A.B')
#set ($spaceReference = $services.model.resolveSpace($spaceName))
#foreach ($result in $services.query.xwql("where doc.space like :space").bindValue('space', "${spaceName}.%").execute())
#set ($resultDoc = $xwiki.getDocument($result))
#if ($resultDoc.documentReference.lastSpaceReference.parent == $spaceReference)
$resultDoc
#end
#end
{{/velocity}}