Selecting random pages in Kentico

Oct 27, 2016
KenticoWeb developmentCMS

Every once in a while, we all need to do something random, and sometimes it’s even related to work. Something we came across here at Ridgeway was a requirement to pick out random documents from a collection to display to the end user. We found a nice little trick to pull content at random from Kentico using a standard pages data source.

Why would you want to do that?

I have to admit, there aren’t a great many occasions where we’ve needed to do this. But the need does arise. Some sample scenarios include:

  • Picking five partner logos to show on your footer
  • Selecting random customer reviews for your product page (5-star of course!)
  • Selecting random articles or products perhaps?

The solution itself is quite simple, though it does need some care and attention where caching is concerned. We’ll need to make some changes to a web part which isn’t really considered best practice in my opinion. Then again, there is a reason that it’s called ‘best practice’ rather than ‘the only practice’.

How to do it

The following steps assume that you have a blank Kentico solution.

  1. Open up the Pages application, and in the root of your site create a folder named Quotes.
  2. Inside this newly created folder, add five or more Page (menu item) pages using the Empty template and give them some random quotes (I.e. Simpson’s quotes) for document names.
  3. Now, back in the root of your site, create a new Page (menu item) with an ad-hoc template and call it Random documents.
  4. In the new page's Design tab, add a Repeater web part and configure it as follows:
    1. Path: /quotes/%/
    2. Page types: Page (menu item)
    3. Select top N pages: 3
    4. Maximum nesting level: 1
    5. Transformation: Page (menu item) > List item
    6. Order by: NEWID()
    7. Columns: DocumentName

That's the first part done. If you either load the live page in preview mode or flip to the Page tab, you should see a list of three random quotes. What you will notice is, this is cached -of course it is, that's the out of the box behaviour and it's good. We want to be bad, however, and remove/disable this cache.

There are a number of ways to achieve this, but the method with least impact is just to disable the cache on the Web Part. To do this, in the Web Part settings, scroll down to System settings and change the Cache minutes to 0. This tells Kentico that we don’t want to cache the content for this control.

Now that you have done this - each time you reload your page, you’ll get a different set of results.

Why does it work?

Under the hood, this is actually quite simple and makes use of an inbuilt SQL function, NEWID(). NEWID() is a SQL function that basically generates a new GUID each time it is called, and given that GUIDs are nice and random, when we sort a set of results by them, we get a different order each time.

If you enable SQL debugging in Kentico, you can see the query that we’re running to get our random quotes.

Conclusion

So, now we have a way of selecting random documents in Kentico using the out-of-the-box features available to Portal developers. This kind of capability makes Kentico’s Portal engine a powerful proposition in my mind. It gives us, as developers, the ability to quickly implement features for our customers on the site without having to jump into the back-end, write custom web parts, and deploy code to the server.

There are two take-away points here:

  1. By looking at the SQL debugging, you can see what Kentico is doing and how it queries the data. You can use this knowledge to fine-tune what you’re doing from a performance point of view.
  2. It’s not always obvious that you can add your own SQL to the data source web parts. If you have unusual requirements, try to bear this in mind and see what you can achieve in SQL. If you need to, you can always create a custom query to get the exact data that you need.