As I have mentioned before Kentico CMS version 6.0 introduced a ton of new features. One of those new features does not get mentioned enough in my opinion. In this blog post I will try to shed some light and how and why you should be using the new Kentico CMS REST Service. The real world example that we are going to walk through building is a very typical home page element, the venerable content slider or image rotator as it is sometimes called. We will use the REST Service to handle retrieving images from the content tree and populating the content for the content slider.
This post requires that you do have some previous knowledge of ASP.NET 4.0, WCF, basic concepts of the REST architectural style, JSON, and jQuery. I also assume that if you are reading this you are familiar with most of the Kentico CMS building blocks, i.e. Documents, Document Types, Custom Tables, Object Classes, Macros, etc. etc.)
Alright the first thing we need to do is make sure that the REST Service is turned on for the site that we are working with. To do so you should follow the instructions located in the Kentico Documentation on how to enable the REST Service. Those instructions are broken out into two different steps, the pre-requisites of allowing a WCF Service to run on your IIS 6 or IIS 7 webserver and the configuration of the REST service itself. There are various configuration options, pay extra attention to the authentication options, for the purposes of this blog post I recommend just allowing read-only access to start.
Now as soon as you have those two steps completed you should be able to browse to the URL that are are working with plus the addition of /rest. If you do browse over to that url the first thing you should get is an authentication prompt. After you enter the correct credentials you should have a page that looks something like this.
If you got this far, that means that the REST Service is correctly configured and ready to go. However, here come my first few tips, the chances of this working perfectly the first time are about 50/50 depending on which ASP.NET Framework versions you have installed on your server or workstation. For me the very first time I tried it I received the following exception. Let's refer to it as the First Issue.
Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, Version=18.104.22.168, Culture=neutral
These two blog posts go into great detail on what causes this issue, but basically it’s a bug with the way WCF has been installed and tweaked as it went from ASP.NET Framework version 3.5 to 4.0. I recommend fixing it by adding the runtimeVersionv2.0 pre condition to the ServiceModel key in the applicationhost.config file. It worked for me. After you modify the config line it should look like this:
Resolution to First Issue
<add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel, Version=22.214.171.124, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler,runtimeVersionv2.0" />
Just save the file and you should no longer have an error. After getting past that exception I was quickly presented with another one:
Error accessing a WCF service: "This collection already contains an address with scheme http. There can be at most one address per scheme in this collection."
Again some research quickly showed that this exception will occur if you have multiple sites in your IIS 7 installation with one or more host headers. As the documentation states it’s easily correctable by adding the following into the main web.config of your Kentico installation:
Resolution to Second Issue
<system.serviceModel> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> system.serviceModel>
After those two tweaks were done. The REST Service in your Kentico installation should come right up. Easy right ?
The last getting started step I recommend is installing the new REST Testing Interface Module that Karol Jarkovsky, Kentico Sr. Solution Architect, released earlier this month. The REST Testing Interface Module is a great new module that allows you to rapidly write and issue REST queries and see the immediate response all in a nice simple test bench style UI. It’s very easy to install, and best of all it is free. If you plan on reading any further with this blog post, stop reading and go download and install it now.
In Part 2 I will continue with tieing up the Kentico CMS REST Service to the front end of the page so that it fills the content of the Content Slider.