Last active
August 29, 2015 14:07
-
-
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
This file contains hidden or 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
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. |
This file contains hidden or 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.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; | |
} | |
} | |
} |
This file contains hidden or 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 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); | |
} | |
} | |
} |
Yeah you don't want to be compiling every time.
Can you post a sample of the ArchetypeModel?
@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 aFunc
, (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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.