Ability to randomly display a space's page

Hi all!

Is there a way to display a random page from a certain space? Being via extension or via a scripting code snippet. I’ve been searching around and have only found a presentation plugin or a image slideshow. Chatgpt was no help either :wink: .

Ideally I wanted to embed a random page out of my “newsletters.webhome” space into my main landingpage, and change it every x minutes if possible.

I’m currently on 13.10.7 via docker

Thanks & stay awesome!

Example script to get randomly a page from the Main space (you should adapt to your space):

{{velocity}}
#set ($xwql = "where doc.space = 'Main'")
#set ($pageReferences = $services.query.xwql($xwql).execute())
#set ($randomIndex = $mathtool.random(0, $pageReferences.size() - 1))
#set ($randomDoc = $pageReferences[$randomIndex])
$randomDoc
{{/velocity}}

Thanks!

Works perfectly.
modified the code for my needs;

#set ($xwql = "where doc.space = 'Newsletters.Newsflashes'")
#set ($pageReferences = $services.query.xwql($xwql).execute())
#set ($randomIndex = $mathtool.random(1, $pageReferences.size()))
#set ($randomDoc = $pageReferences[$randomIndex])

{{display reference="$randomDoc"/}}

I’m guessing refreshing that part (I’ve embedded it in a dashboard) is a bad idea?

cool :slight_smile:

Not sure I understand what you mean.

Hi All, I would like to share my final code for future reference.
Above vode works fine, but when the user has admin rights, the page sometimes causes a redirect to the webpreferences page. Fixed it.

#set ($xwql = "where doc.space = 'Newsflashes'")
#set ($pageReferences = $services.query.xwql($xwql).execute())
#set ($randomIndex = $mathtool.random(1, $pageReferences.size() - 1 ))
#set ($randomDoc = $pageReferences[$randomIndex])
###$randomDoc

#if($randomDoc=="Newsflashes.WebPreferences")
  #set ($randomDoc = "Newsflashes.WebHome")
#end
{{display reference="$randomDoc"/}}

Cheers and thanks for all the help!

Thanks for posting the solution. Don’t hesitate to post on https://snippets.xwiki.org/xwiki/bin/view/Main/WebHome :slight_smile: