Skip to content

Instantly share code, notes, and snippets.

@joeriks
Last active January 17, 2017 00:47
Show Gist options
  • Save joeriks/5964810 to your computer and use it in GitHub Desktop.
Save joeriks/5964810 to your computer and use it in GitHub Desktop.
Umbraco Content as Json (alttemplate=json or alttemplate=json&includeChildren=1 or alttemplate=json&includeAllChildren=1) for Umbraco v 4
<%@ Master Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="umbraco" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
var createdObject = nodeObject(umbraco.NodeFactory.Node.getCurrentNodeId(), Request["includeChildren"] != null, Request["includeAllChildren"] != null);
//var filter = Request.QueryString.Cast<string>().Select(s => new KeyValuePair<string, string>(s, Request.QueryString[s]));
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(System.Web.Helpers.Json.Encode(createdObject));
Response.End();
}
private object nodeObject(int nodeId, bool includeChildren, bool includeAllChildren)
{
var node = new umbraco.NodeFactory.Node(nodeId);
return new
{
node.Name,
node.UrlName,
node.NodeTypeAlias,
node.CreatorName,
TemplateId = node.template,
Properties = node.PropertiesAsList.Select(p => new { p.Alias, p.Value }).ToDictionary(k => k.Alias, k => (k.Value.Contains("UMBRACOMACRO") ? umbraco.library.RenderMacroContent(k.Value, nodeId) : k.Value)),
node.CreateDate,
node.UpdateDate,
node.SortOrder,
node.Url,
ParentId = (node.Parent != null) ? node.Parent.Id : -1,
ChildIds = node.ChildrenAsList.OrderBy(n => n.SortOrder).Select(n => n.Id),
Children = (includeChildren || includeAllChildren) ? node.ChildrenAsList.OrderBy(n => n.SortOrder).Select(n => nodeObject(n.Id, includeAllChildren, includeAllChildren)) : null
};
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment