Skip to content

Instantly share code, notes, and snippets.

@jamiepollock
Created September 21, 2015 14:58
Show Gist options
  • Save jamiepollock/fc69044cf874bd838d76 to your computer and use it in GitHub Desktop.
Save jamiepollock/fc69044cf874bd838d76 to your computer and use it in GitHub Desktop.
Custom Ditto ValueResolver - Single IPublishedContent from IEnumerable<IPublishedContent
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Our.Umbraco.Ditto;
using Umbraco.Core.Models;
namespace My.Website.ComponentModel
{
public class SinglePublishedContentUmbracoPropertyAttribute : UmbracoPropertyAttribute
{
}
public class SinglePublishedContentValueResolver : UmbracoPropertyValueResolver
{
public override object ResolveValue()
{
var value = base.ResolveValue();
if (value is IEnumerable<IPublishedContent>) {
return (value as IEnumerable<IPublishedContent>).FirstOrDefault();
}
throw new InvalidCastException(string.Format("Invalid value resolver! The value must be of type {0}", typeof(IEnumerable<IPublishedContent>)));
}
}
}
@protherj
Copy link

protherj commented Dec 9, 2015

Found this while Googleing and didn't have any context. For future Googlers, this is the thread that this code was born from:

https://our.umbraco.org/projects/developer-tools/ditto/ditto-feedback/71581-ienumerableipublishedcontent-to-single-ipublishedcontent-dittovalueresolver-or-typeconverter

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