Displaying related documents by tags

5/3/2016 4:49:34 PM

One of the most useful and under utilized web part of Kentico web parts repository is Random Page(s) web part. With the help of this there is no more need to write complex queries or logic to show Random documents on every page load.

Suppose you have a requirement where you would like to show 3 Random articles/blogs on the basis of the current Tags, category or any other property associated with an article. For this example, I will only focus on getting random documents on the basis of Tags. This feature is quite common on any website. I will also show through a separate example that how can you use this to fetch articles based on any other property too.

Another use case of this web-part could be showing related random products based on the product that a user has selected. For example, on an e-commerce website you could use this webpart to related but random products like iPhone 6 which would be iPhone 6s, iPhone 5s and so on.

Things that you will need to accomplish this:-

1. Kentico Random Document Webpart
2. An sql query to extract similar documents as per your requirement. In this case we are fetching documents that are sharing same tags.

Just go to the page template where you would like to add this web part. Click on a suitable Zone to add a new web part. Search for "Random" and select Random Page web part.

Random Page Webpart


After adding this webpart, we need to configure from where to pick the data from(Path) and other settings like page types, number of pages/documents that you want to show

In my case, I set these values to following. I used Kentico out of the box CMS.BlogPost page type. This provided me enough functionality to write my blogs.

Random Page Settings

The brain of this webpart lies in the business logic that you will need to show related documents. In my case I used tags to show similar documents.

DocumentID IN (SELECT DocumentID FROM CMS_DocumentTag WHERE TagID IN (SELECT TagID FROM CMS_DocumentTag WHERE DocumentID = {%DocumentID%}))

The query above will go into my where clause in the screen shot below.



You will have to add transformation as per your need to how you want to show your articles.

Pro Tip 1:-  DocumentID IN (SELECT DocumentID FROM CMS_DocumentTag WHERE TagID IN (SELECT TagID FROM CMS_DocumentTag WHERE DocumentID = {%DocumentID%})) AND DocumentID != {%DocumentID%}

The bold section in this query will make sure that you are not showing current document as the suggested document by making sure DocumentID is not equal to Current document's id.

Below is an example of how we did for our client. "Cooperative" & "Member" are the Tags associated with the current article and articles shown under "You may like this" has articles which one or both tags associated with it. Since we set the web part configuration to show only 3 artciles that is why it showing only three articles.




Pro Tip 1:-  Kentico uses pages' output caching to increase the performance of a page. In order for this webpart to show different documents on each page load for every user it is essential that we override the caching and set it to 0. The one good thing here is, that we don't need to set caching 0 for an entire page but for only this web part.

Go to the performance section of the webpart and you will see something like this. By default, cache minutes value would be left empty. We need to set it to zero so that we don't see cached content on multiple page loads.



That's it. We are done.

In next part of this article I will cover how can we use Kentico categories to build relationships and show related articles.

You may read more about Kentico Caching here

You may also read about Tagging documents here.