Kentico Cloud client side solution

#Kentico development #Kentico Kontent

SHARE

 
Last year’s we can see a huge shift towards client side solution in the world of web. Development of JavaScript frameworks like Angular JS, React JS allows us to build rich and interactive UI, moving more and more business rules to a client. This change introduces new requirements to a server and highlights a need in lightweight and stateless solutions. One of such solutions is Kentico Cloud–new warehouse for your content.

 

Kentico Cloud is platform agnostic content management cloud solution. That means that you could consume it from any platform: mobile native applications, web site, Windows application using any programming language you like.

In one of my previous posts I suggested server side approach for fetching data from Kentico Cloud. In this post, I’m going to focus on retrieving of a content with JavaScript and some JS frameworks.

 

Kentico Cloud API

Kentico Cloud exposes REST API, that provides an interface to get content. It allows getting list of content items or a single item, list of content types or a single content type structure. Basically, we need to send a GET request and specify data/content we want to receive via query parameters in the request URL. We could specify content type of items we are requesting, columns (elements), order of items, how many items to skip and/or take, etc. Kentico Cloud API allows only reading data. All the details are available in the documentation.

 

So, we are going to implement client side (JavaScript) functionality that will grab some content stored in Kentico Cloud. Before we begin here is what we need in order to rich our goal:

  • Kentico Cloud account – there is a free version, that limits the amount of API call, but it is more than enough in our case
  • Create some project in Kentico Cloud
  • Create at least one content type
  • Create a couple of content items
  • Get project ID at Kentico Cloud – we need to include it into request URL
  • Text editor
  • Browser 😊

For this particular sample, we don’t need any server side logic, so we are good to create simple HTML page with some markup and JavaScript functionality that will do the magic.
 

JS Code

Let’s take a look at pure JavaScript function that retrieves content:
 

function loadContent() {

      var xhttp = new XMLHttpRequest();

      xhttp.onreadystatechange = function() {

        if (this.readyState == 4 && this.status == 200) {

            var data = this.response;

            for (var i=0; i<data.items.length;i++){

                document.getElementById("demo").innerHTML += "Name: " + data.items[i].elements.name.value + "<br/>";

                document.getElementById("demo").innerHTML += "Descr: " + data.items[i].elements.description.value + "<br/>";

            }

        }

      };

      xhttp.responseType = 'json';

      xhttp.open("GET", "https://deliver.kenticocloud.com/6dea43b2-30ae-4f99-8da8-c29d0caa3870/items?element.sampletype", true);

      xhttp.send();

    }


See the entire implementation here.

Here is jQuery AJAX call that fetches content:
 

$.ajax({

      url: "https://deliver.kenticocloud.com/projectId/items?element.sampletype",

      method: 'GET',



      success: function(response) {

        var markup = "";

        $.each(response.items, function(i, el) {

          markup += "Name: " + el.elements.name.value + '<br/>';

          markup += "Descr: " + el.elements.description.value + '<br/>';

        });

        $("#demo").html(markup);

      }

    })


See the entire implementation here.

 And the last one is Angular JS code that does, in fact, what two previous do:
 

$scope.dataSvc = $resource('https://deliver.kenticocloud.com/projectId/items?:contenttype&elements=:elements&order=:order&depth=:depth&skip=:skip&limit=:limit', {

    depth: 0,

    skip: 0,

    limit: 100000

  });

See the entire implementation here.

Looks simple, right? Someone might say that there are no doubts that we could consume Kentico Cloud API with JavaScript or JS frameworks. Yes, but let’s take a closer look into the Angular JS implementation: it creates a single resource factory that allows getting any content by specifying appropriate parameter like this:
 


$scope.dataSvc = $resource('https://deliver.kenticocloud.com/projectId/items?:contenttype&elements=:elements&order=:order&depth=:depth&skip=:skip&limit=:limit', {
    depth: 0,
    skip: 0,
    limit: 100000
  });

$scope.loadData = function(params) {

    $scope.dataSvc.get(params

    , function(response) {

      $scope.data = response.items;

    });

  }

  

  $scope.loadAll = function(){

    $scope.loadData({contenttype: 'element.sampletype'});

  }

  

  $scope.loadFirst = function(){

    $scope.loadData({contenttype: 'element.sampletype', limit: 1});

  }

  

  $scope.loadSecond = function(){

    $scope.loadData({contenttype: 'element.sampletype', skip: 1, limit: 1});

  }

  

  $scope.loadName = function(){

    $scope.loadData({contenttype: 'element.sampletype', elements: 'name'});

  }


As you can see I'm calling $scope.LoadData method with different parameters each time to get different results. 

This is the beauty of Kentico Cloud and the main point of this post: it is possible to implement a centralized access point to all the data stored in Kentico Cloud. Genius is simplicity!

 

Author

Check other articles

Bitsorchestra
5 5

What our clients say

Bits Orchestra team are outstanding developers​. They listen carefully to our business needs and easily turns our business objectives into a well thought out and executed development effort. Roman is very bright and definitely the most capable developer that has worked on our site. He is not only a Kentico expert but has successfully tackled other complicated development assignments demonstrating expertise in both front and backend development. Roman takes initiative to suggest enhancements that make site maintenance easier while improving the customer experience. The team is very responsive to our work requests and has great follow up. They have also worked very business partners and this has reflected positively on our company. Roman is a true partner for us and a tremendous asset to our organization. We will continue to work with them and would highly recommend Roman and his team for your development needs. He and his team will exceed your expectations!
 Alan Lehmann
Alan Lehmann
President at In energy sector

What our clients say

The Bits Orchestra team does excellent work. They are always available and I appreciate our frequent calls and screen-shares together. Their dedication to the projects and knowledge of Kentico is outstanding. They truly care about the quality of their work, and became a part of our team easily!
Shena Lowe
Shena Lowe
Managing Partner at Consensus Interactive

What our clients say

We hired Roman for a Kentico analysis project and have been very satisfied. He is very skilled and professional. We are looking to hire him and his team again on future projects.
Sylvain Audet
Sylvain Audet
CEO at MyDevPartner.com

What our clients say

Roman and team have taken over an existing Kentico EMS site for a large US Oil Company. So far, they have handled every single request that we have thrown at them and these were diverse, challenging, often bespoke, usually urgent and almost daily, over the last 11 months. Their work is of an extremely high quality, they are capable, quick and we have great confidence in the support that we are getting.
Jon Hollis
Jon Hollis
Head of Web Development at confidential

What our clients say

Bits Orchestra team was very helpful, they had a good understanding of the brief and deep knowledge of the system. They were always keen to provide advice and recommendations that benefit the project substantially.
Ramon Lapenta
Ramon Lapenta
Senior Front End Developer at Cyber-Duck Ltd