Skip to content

Instantly share code, notes, and snippets.

@zpqrtbnk
Last active December 22, 2015 23:49
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 zpqrtbnk/6549963 to your computer and use it in GitHub Desktop.
Save zpqrtbnk/6549963 to your computer and use it in GitHub Desktop.
Umbraco strongly typed published content experiments
// demo of a basic strongly typed published content for an article
// you just need to drop the following class anywhere in your code
// and every strongly typed methods returning IPublishedContent will
// actually return an Article instance (which is an IPublishedContent)
// we want the class named Article though the content type alias is 'newsArticle'
[PublishedContentModel("newsArticle")]
public partial class Article : PublishedContentModel
{
public Article(IPublishedContent content)
: base(content)
{}
// that syntax may be verbose BUT it's fast and should be generated anyway
public string Title { get { return this.GetPropertyValue<string>("title"); } }
public string BodyText { get { return this.GetPropertyValue<string>("body"); } }
}
// use it
public class JustTesting
{
public void Test()
{
// get the titles of the articles at the root
var rootContent = UmbracoContext.Current.ContentCache.GetAtRoot();
var rootArticles = rootContent.OfType<Article>();
var titles = rootArticles.Select(a => a.Title);
// get the titles of all the articles below node 1234
var node = UmbracoContext.Current.ContentCache.GetById(1234);
var articles = node.Descendants<Article>();
titles = articles.Select(a => a.Title);
}
}
// and of course in just any Razor view:
@model.Title
@model.Children<SubArticle>.DateOfFirstRelease.ToString("yyyyMMdd");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment