Skip to content

Instantly share code, notes, and snippets.

@warrenbuckley
Created March 17, 2012 21:25
Show Gist options
  • Save warrenbuckley/2065415 to your computer and use it in GitHub Desktop.
Save warrenbuckley/2065415 to your computer and use it in GitHub Desktop.
Work In Progress - JSON Content Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//WB Added
using Umbraco.Cms.Web;
using Umbraco.Cms.Web.Context;
using Umbraco.Framework.Persistence.Model.Associations;
using Umbraco.Hive;
using Umbraco.Hive.RepositoryTypes;
using Umbraco.Cms.Web.Model;
using Umbraco.Framework;
using Umbraco.Framework.Context;
namespace JSONController.Controllers
{
public class JSONController : Controller
{
private readonly IUmbracoApplicationContext context;
public JSONController(IUmbracoApplicationContext context)
{
this.context = context;
}
public JsonResult GetContentById(HiveId contentID)
{
//Get the node
var node = context.Hive.Cms().Content.GetById(contentID);
//Return as JSON
return Json(node, JsonRequestBehavior.AllowGet);
}
public JsonResult GetContentByNodeName(string nodeName)
{
var node = context.Hive.Cms().Content.SingleOrDefault(x => x.Name == nodeName);
//Return as JSON
return Json(node, JsonRequestBehavior.AllowGet);
}
public JsonResult GetContentByDocType(string doctypeAlias)
{
var nodes = context.Hive.Cms().Content.Where(x => x.ContentType.Alias == doctypeAlias);
//Return as JSON
return Json(nodes, JsonRequestBehavior.AllowGet);
}
}
}
@drobertson123
Copy link

Warren

Thanks for these. Is this JSON controler working for you in v5.1(CMS)?

Doug

@warrenbuckley
Copy link
Author

Hi Doug,
This was very much a proof of concept and was untested.

Warren :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment