KendoUI + WebApi + Jsonp - how to get KendoUI working with WebApi and Jsonp

Most people know that the KendoUI controls are awesome and free. The thing that is missing from there demo site is how to create a Jsonp WebApi service that the controls can talk to. I tried to get it up and running and ran into an issue because my service was returning regular Json and the “Loading” icon just spun for ever.

Here’s how to get an e2e going with KendoUI TreeView and WebApi. This post very intentionally follows the code demo provided by Telerik here

Hopefully you find it useful.

Download Latest version from GitHub

Create VS Project
1. Create a new MVC project.

Add Nuget References
2. Add the following references

WebApiContrib.Formatting.Jsonp – This adds the JsonpMediaTypeFormatter class to the project.

KendoUIWeb – This adds the KendoUI controls to the project.

Register Formatter
3. Add the following line of code to the Application_Start() method of the Global.asax file

1
GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonpMediaTypeFormatter());

Create Model

4. Add the following Employee class to the /Models folder

1
2
3
4
5
6
7
public class Employee
{
public int EmployeeId { get; set; }
public string FullName { get; set; }
public bool HasEmployees { get; set; }
public int? ReportsTo { get; set; }
}

Create Controller

5. Add the following Controller to the /Controllers folder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class EmployeesController : ApiController
{
private readonly List<Employee> _employees = new List<Employee>
{
new Employee{ FullName = "Jon Gallant", EmployeeId = 1, HasEmployees = true, ReportsTo = null},
new Employee{ FullName = "Scott Hanselman", EmployeeId = 2, HasEmployees = false, ReportsTo = 1},
new Employee {FullName = "Howard Dierking", EmployeeId = 3, HasEmployees = false, ReportsTo = 1},
new Employee {FullName = "Drew Miller", EmployeeId = 4, HasEmployees = false, ReportsTo = 1},
new Employee { FullName = "Jeff Atwood", EmployeeId = 5, HasEmployees = false, ReportsTo = 1}
};
public IEnumerable<Employee> Get()
{
return _employees.Where(e => !e.ReportsTo.HasValue);
}
public IEnumerable<Employee> Get(int employeeId)
{
return _employees.Where(e => e.ReportsTo == employeeId);
}
}

Create View

6. Add the following View to the /Views/Home/Index.cshtml file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<link href="~/Content/kendo/2013.1.319/kendo.common.min.css" rel="stylesheet" />
<link href="~/Content/kendo/2013.1.319/kendo.default.min.css" rel="stylesheet" />
<script src="~/Scripts/kendo/2013.1.319/jquery.min.js"></script>
<script src="~/Scripts/kendo/2013.1.319/kendo.web.min.js"></script>
<div id="treeview" class="demo-section"></div>
<script>
//var serviceRoot = "http://demos.kendoui.com/service";
var serviceRoot = "api";
homogeneous = new kendo.data.HierarchicalDataSource({
transport: {
read: {
url: serviceRoot + "/Employees",
dataType: "jsonp"
}
},
schema: {
model: {
id: "EmployeeId",
hasChildren: "HasEmployees"
}
}
});
$("#treeview").kendoTreeView({
dataSource: homogeneous,
dataTextField: "FullName"
});
</script>

Run it!

Run it and this is what you should see.

Jon

Share