Skip to content

Instantly share code, notes, and snippets.

@jamiepollock
Created November 27, 2013 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiepollock/7684166 to your computer and use it in GitHub Desktop.
Save jamiepollock/7684166 to your computer and use it in GitHub Desktop.
A demo Umbraco v6+ UmbracoApiController (an Umbraco Context wrapped WebApiController). Each request returns an array of strings which would typically be read in by a JavaScript library as JSON.
using System;
using System.Collections.Generic;
using Umbraco.Core.Models;
using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi;
namespace UmbracoDemo.Controllers
{
[PluginController("JsonDropDownList")]
public class JsonDropDownListApiController : UmbracoApiController
{
// /umbraco/
public IEnumerable<string> GetData()
{
return new[] { "Table", "Chair", "Desk", "Computer", "Beer fridge" };
}
public IEnumerable<string> GetDataFromEnum()
{
return Enum.GetNames(typeof(ProjectStates));
}
public IEnumerable<string> GetDataFromUmbraco()
{
//Change ID to whatever node has the child nodes you wish to select.
//In a production site this would be a parameter of the function
var rootId = 1054;
var children = this.Services.ContentService.GetChildren(rootId);
foreach (IContent child in children)
{
yield return child.Name;
}
}
}
//Included in the same file for readability
public enum ProjectStates
{
Planning,
Development,
InternalQA,
UAT,
Complete
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment