Created
November 27, 2013 22:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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