JSON output in Umbraco 5

510 views Asked by At

I just switched from Umbraco 4 to Umbraco 5 and it seems like a lot has changed. So what i basically want is a possibility to add a alternative template to my document-types. The fall back template shall return the content as JSON.

Why, you say? I want to create an API-like way of accessing the Umbraco data from my mobile devices.

WebAPI (http://cultiv.nl/blog/2012/4/22/exposing-umbraco-5-content-through-the-aspnet-web-api/) I have thought about using WebAPI from asp.net MVC 4, but the project is really just a proof of concept and i don't want to code each endpoint.

So i found a som guys that did a package for Umbraco 4 that actually does this and renders the content of @currentPage as Json. The template is hit by adding "/JSON" to the end of the url. Unfortunetly this uses xslt, which ihas been removed from Ubraco 5.1 (Good thing).

So. I bet it's simple to create a partial, a macro or a partial macro that does this and add it to a template. But is just cant figure out where to start.

Any help with that? What I'm looking for is a step guide on what steps to take, to make the setup. Rendering the stuff ad json in C# i can handle. It's the integration into Umbraci I lack.

Hoe u can help.

1

There are 1 answers

1
thoehler On

The alternative templating works exactly like in v4: Just add the name of the template at the end of the url: yoururl/json

Then add a new razor view to the templates named "json" with the following code:

@inherits RenderViewPage
@using System.Web.Mvc.Html;
@using Umbraco.Cms.Web;
@{
    Layout = "";
}
{
@foreach (var attribute in Model.Attributes)
{
    string.Format("\"{0}\": \"{1}\"", attribute.AttributeDefinition.Alias, attribute.DynamicValue);
}
}

This code can be used as a starting point without using the web api or own controllers.

Don't forget to allow the tempplate on all document types

hth, Thomas