Free Web Part for Kentico - Related Blog Posts by Tag
IntroductionDo you want to know what my favorite web part of all time, in the entire galaxy, across any version of Kentico is? The title of this blog post may have given it away to the astute Kentico developer, but you might be surprised at how one of the oldest and simplest of web parts is still super useful to this day. That's right, ladies and gentlemen, in my opinion, the champion of all Kentico web parts is...
One Web Part to Rule Them All
... the Repeater with Custom Query. Not only can this web part display anything in your Kentico CMS website, it can also be customized like crazy with a few clicks of the mouse. When I say anything I mean it. It doesn't matter if your data lives in the content tree of the pages application, in a custom table object, in another database table that Kentico doesn't know about, or even in another database catalog on your server. It can show anything that you have tied to your Kentico site database-wise or content-wise.
Because Kentico is super flexible you can also pre-configure the properties of this repeater, clone it as new web part, and re-use your customized repeater in any web part zone you wish. This is one of my favorite parts of Kentico. We don't have to write any code to do this. I am going to show how easy it is to use this repeater to derive a new pre-configured web part in this post. I hope it illustrates why I love using custom query repeaters.
How It WorksTo start you should have some familiarity with how web parts work in Kentico because we are going to build a new one. If you do not have this experience check out the documentation on creating web parts for Kentico 8. I am also assuming you know how to tag a document in Kentico for your Kentico blog. I am not going to review that because it is on the Form tab of just about every blog post page type out there.
Basically you just have to clone the Repeater with Custom Query web part and give it a unique code name. Just click the Clone button from the expanded menu (the 3 small dots next to the red trash can icon) on the original web part to bring up the clone object dialog menu. For this example I have chosen Related Posts by Tag as my unique name object code name, and I have un-checked the clone web part files checkbox because we are not touching any C# code today. Also remove the _1 from the file name property.
After updating the values it should look like:
After you click the save Clone button you are brought to general properties of your new web part. If you forget to rename the Web part file name at this step (just like I did in the screen shot above) you can simply select queryrepeater.ascx from selector.
And here is where the secret sauce is, all of the properties and system properties of the web part are available to customize. You can modify all of the default values to have information pre-populated on your web part for when you add it to a page template. If there are too many properties that you do not want your content admins to see you can easily hide the single property or the entire category of properties by un-checking the Display attribute in editing form checkbox. I have done that for multiple properties to make my new web part from the existing one.
The only other notable aspect of this web part is the query that runs behind the scenes. Again because Kentico already has built in tagging support for blog posts or any other kind of page types this is fairly simple as well. The query is structured to find tags that match the current document that the page is rendering for. I had the help of our awesome senior developer and all around SQL guru, Mark Schmidt, at BizStream on this one.
Mark wrote a simple weighting system on which tag is used the least in order to generate matches based on the uniqueness of the tag used on each document. What this does is filter out the tags that are used more often to provide the most interesting results of finding related blog posts by tags. If you don't like this idea you can adjust the query to meet your matching needs.
You can simply add this new web part to your page template and everything should be good to go, you do not need to re-setup the properties every time.
The ResultsOnce you have imported the web part and added it to the page template that shows your blog posts you should be good to go. The screen shot below shows how the output of the web part appears. You can also see this web part live and in action on the bottom of any blog post here at Mcbeev.com because this is the exact web part I used to display related content based on what blog post you are viewing.
If a blog post is tagged like so:
Then as long as you look at another blog post that uses the same tag you should get the top 6 results like so:
Pro Tip: Showing related content by interest is a good way to reduce bounce rate on your web site.
DownloadYou can download a copy of the Kentico CMS Related Blog Posts by Tag web part here, as well as at the Kentico Marketplace. I have tested the web part in versions 8.0, 8.1, and 8.2. But if you follow these simple instructions you can easily make this web part work in pretty much any version of Kentico.
The only small tweak you may need to do is to adjust the default query and/or the default transformation for the web part if you don't use the standard CMS.BlogPost page type for your blog. The current web part relies on using the Blog Post Title field and Blog Post Teaser field for the thumbnails.
ConclusionIn just a few simple steps I have shown you how flexible and powerful the Repeater with Custom Query web part is. This again is the main reason it is my favorite web part of all time. I was able to setup a brand new set of functionality just be re-using this core concept in Kentico.
Feel free to leave any feedback on the web part in the comments on this post. I will try to support it as best I can over email or comments. And hey if you have a favorite web part in Kentico let me know here too.