Random seed in search result webpart query
-
19-01-2021 - |
Question
I am looking for a way to randomize a result table. I used to do this via REST-API like this
var randomSeed = parseInt((Math.random() * 1000000), 10),
searchUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext=%27INT%27&sourceid=%27b09a7990-05ea-4af9-81ef-edfab16c4e31%27&sortlist=%27[random:seed=" + randomSeed + "]:ascending%27&rowlimit=5&selectproperties='OrgNames,PreferredName,PictureURL,Department,JobTitle,Path,AboutMe'";
Unfortunately I need to use a search result webpart from now. The reason doesn´t matter. The only way I see is to set the row maximum to 1 and set the page random. But this causes a new Url with some queries in. Is there a way to set the seed parameter in the dataprovider and use dataprovider.issueQuery()? I can´t find a setting or property.
THANKS!
Solution
I did it this way:
Only one result per page. This leads me to 3000 pages. In my control template I added
if(!document.hasNewElement) {
ctx.ClientControl.page(parseInt((Math.random() * ctx.DataProvider.get_totalRows()), 10));
document.hasNewElement = true;
}
On the first load I change the page and set a new property on the document element. After the page has changed the control will not run into the code again due to the property. So every reload there will be a new element to show up.