Go to main content

Create JSON API endpoints in the Kentico Portal Engine

You might have noticed that Kentico released a new product Kentico Cloud. It has a fancy UI for managing content and easy-to-use API endpoints talking to you in JSON. This gives you a lot of flexibility and you can render and manipulate data on the server or the client side.

Create JSON API endpoints in the Kentico Portal Engine

In Kentico CMS/EMS you can operate data only server side by default. However, with a little effort you can create a JSON API endpoints in Kentico Portal Engine that allow you to work with data on the client side.

Why would I need endpoints in Kentico CMS/EMS?

Requesting data on the client side with AJAX is fast and invisible to a website visitor. Having data in the JSON format allows you to easily use javascript libraries that make your data interactive. This all could be achieved in the Kentico Portal Engine with use of web parts and QueryString macros.

Normally, in Kentico you communicate with server via postbacks which is not really fast and responsive. Moreover, this requires you to code in the Kentico back-end.

What is an endpoint?

An endpoint represents a service that is approachable by a URL.  

 http://localhost/items?documentname=example

Really simple example. Imagine you would request this endpoint via jQuery.ajax().

jQuery.ajax({ 
  url: 'http://localhost/items?documentname=example', 
  type: 'GET', 
  dataType:'json', 
  error: function(e) {console.error(e);}, 
  success: function(data) {console.log(data.items);}
});  

And you expect a JSON response processed by jQuery so you end up with an array of objects.

[ { documentID: 1, nodeAliasPath: "/example" }, { documentID: 2, nodeAliasPath: "/page/example" } ] 

How to make this happen in the Kentico Portal Engine?

The core of the matter is the /items page that takes the documentname url parameter and returns a list of pages with specified DocumentName.

If you think of using the Repeater web part you're almost right. However, if you use just the Repeater web part, you will get a full page output with all the html tags like <html><head>...</head><body>...</body></html>. But we need pure JSON.

Kentico has a magic web part called Custom response that returns pure output with specified content type. Unfortunately, the web part is able to return only static text. We need a repeater with the magic of the Custom response web part.

I have prepared 2 web parts that combine Custom response and the Repeater/Repeater with custom query web parts. You can download and import packages of those web parts for Kentico 10.0.13:

With use of the web parts you can get data from Kentico as you normally would. If you set Content type to JSON and specify Content before, Content after and Transformation web part properties to render data in JSON format, your endpoint is ready to use.

Practical example

  1. Download and import the Custom response repeater and/or Custom response repeater with custom query web part in your Kentico project.
  2. In the Pages application create a page. Set Document name to items. Place the web part in there.
    1. Content before: {"items": [
    2. Content after: ]}
    3. Transformation (Text/XML):
      {"documentID": {% DocumentID %}, "nodeAliasPath": "{% NodeAliasPath %}"}{% DataItemCount - DataItemIndex == 1 ? "" : ", " %}
    4. Content type: application/json
    5. Where condition:
      {% QueryString.documentname != "" ? "DocumentName='" + SQLEscape(QueryString.documentname) + "'" : "" #%}
    6. Set Path and/or Page type as well to define what content should be rendered.
  3. On other page that is intended to show the data place the Javascript web part with code that calls AJAX request. Example:
jQuery.ajax({ 
  url: 'http://localhost/items?documentname=example', 
  type: 'GET', 
  dataType:'json', 
  error: function(e) {console.error(e);}, 
  success: function(data) {console.log(data.items);} 
}); 

Make sure the jQuery library is included. When you request the page you should see output in the browser console.

Final thoughts

Hope the article explained the concept of endpoints in the Kentico Portal Engine well. From this point you should be able to adjust the concept to your scenario.

Notes

  • Always make sure your Where conditions in repeaters that use QueryString are safe from SQL injections. Read article from Juraj Komlosi.
  • The {% DataItemCount - DataItemIndex == 1 ? "" : ", " %} expression in the transformation renders the ", " string for all items except the last one. {% DataItemCount - DataItemIndex == 1 %} is an alternative for the <%# IsLast() %> method in ASCX transformations.
Milan Lund

is a freelance web developer and a proud Basenji owner. His specialties are Kentico CMS/EMS and Kentico Cloud.

Further reading

all posts
  • Kentico CMS & EMS

    Custom macro: Get the current user's country and state

    In this post I will provide you with a code example of a custom macro that gives you country and state of the current user.

  • Kentico CMS & EMS

    Do not run Kentico in a shared hosting environment

    Usually, when you want to run your small website you expect low requirements for a web hosting environment and you probably think of an economic shared web hosting service. Unfortu…

  • Kentico CMS & EMS

    Easter eggs in Kentico

    What is really great about having friends among people who create Kentico is not only I have someone I can play sports with or hang out. They also keep me updated about what’s happ…