Skip to content

Instantly share code, notes, and snippets.

@jamiepollock
Last active August 29, 2015 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jamiepollock/6cfd46323661aa2fa157 to your computer and use it in GitHub Desktop.
Save jamiepollock/6cfd46323661aa2fa157 to your computer and use it in GitHub Desktop.
Umbraco GetPropertyValueRecursively with custom logic to determine if a recursive value is valid
The following code is intended for use as a replacement for the GetPropertyValue<T>(string alias, bool recursive) method when getting strongly typed values back.
This example uses Archetype (https://github.com/imulus/Archetype/tree/master/app/Umbraco/Umbraco.Archetype) where in Umbraco v7.1.8 calling such a property value recursively returns an ArchetypeModel with 0 Fieldsets.
There could be other possible applications for allowing a developer to determine the logic when GetPropertyValue<T>(string alias, bool recursive) is trying to get a value recursively but accepts it.
using System;
using System.Linq.Expressions;
using Umbraco.Core.Models;
using Umbraco.Web;
namespace My.Website.ExtensionMethods {
public static class UmbracoExtensions {
public static TPropertyValue GetPropertyValueRecursively<TPropertyValue>(this IPublishedContent content, string alias, Expression<Func<TPropertyValue, bool>> recursiveValueIsValidExpression, TPropertyValue defaultValue = default(TPropertyValue)) {
if (content.HasProperty(alias)) {
var value = content.GetPropertyValue<TPropertyValue>(alias);
if (recursiveValueIsValidExpression.Compile().Invoke(value)) {
return value;
}
}
if (content.Parent != null) {
return content.Parent.GetPropertyValueRecursively(alias, recursiveValueIsValidExpression);
}
return defaultValue;
}
}
}
using Archetype.Models;
using My.Website.ExtensionMethods;
using System.Linq;
using System.Web.Mvc;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
namespace My.Website.Controllers {
public class MyPageController : RenderMvcController {
public override ActionResult Index(RenderModel model) {
var propertyGotRecursively = model.Content.GetPropertyValueRecursively<ArchetypeModel>("archetypeBuiltProperty", x => x.Fieldsets.Any());
var viewModel = new {
MyValue = propertyGotRecursively
};
return View(viewModel);
}
}
}
@zpqrtbnk
Copy link

So it walks up the tree until a content has the property with the proper alias, and a value satisfying the "is valid" method, correct?
Question: why use an Expression<Func> and compile it? And not directly a Func? Not sure there's a benefit, and here you're re-compiling the expression on each content.

@JimBobSquarePants
Copy link

Yeah you don't want to be compiling every time.

Can you post a sample of the ArchetypeModel?

@leekelleher
Copy link

@jamiepollock - I think it is a good and very useful extension method.

I did a fork and made some modifications:

  • Made it an overload of the existing GetPropertyValue<T>.
  • Replaced the Expression with a Func, (as noted by @zpqrtbnk)
  • Included the defaultValue in the call to the parent content node.

https://gist.github.com/leekelleher/8e85a1020179edfad29a

Feel free to do what you want with it. :shipit:

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