GetPropertyValue issues using Nested content and MNTP
Hi all,
I am facing some strange issues with Nested Content and GetPropertyValue.
In other projects ( < v7.6.x ) I was using the following code to get the Id of the IPublishedContent assigned to a property on the NC Document Type:
public static bool CheckCategory(this IPublishedContent node, int category)
{
// Nested content property which contains a list of the assigned packages
var packages = node.GetPropertyValue<IEnumerable<IPublishedContent>>("packages");
return packages.Select(p => p.GetPropertyValue<int>("category")).Contains(category);
}
But this doesn't work anymore in a project running 7.7.9.
I had to change the code to the following in order to make this work.
But I don't like it and prefer to old way which was excellent.
public static bool CheckCategory(this IPublishedContent node, int category)
{
// Nested content property which contains a list of the assigned packages
var packages = node.GetPropertyValue<IEnumerable<IPublishedContent>>("packages");
var result = packages.Select(p => p.GetPropertyValue<IEnumerable<IPublishedContent>>("category").First().Id);
return result.Contains(category);
}
The property Category is a Multinode Treepicker which is setup to only have one node.
in Umbraco 7.6 Property Value Converters are on by default, which is why your MNTP is returning IEnumberable
but equally the default MNTP (MultiNodeTreePicker2) in Umbraco 7.6 stores UID values not Int values - so if you are using the new pickers you won't be able to get to the int values.
If you want to still use the value converters (or if you using the new pickers) you can simplify the query a bit.
var packages = Model.GetPropertyValue<IEnumerable<IPublishedContent>>("packages");
var contains = packages
.Any(x => x.GetPropertyValue<IEnumerable<IPublishedContent>>("category").Any(y => y.Id == category));
there is probably some clever xpath that does this really fast too - but the above will do it.
GetPropertyValue issues using Nested content and MNTP
Hi all,
I am facing some strange issues with Nested Content and GetPropertyValue.
In other projects ( < v7.6.x ) I was using the following code to get the Id of the IPublishedContent assigned to a property on the NC Document Type:
But this doesn't work anymore in a project running 7.7.9.
I had to change the code to the following in order to make this work. But I don't like it and prefer to old way which was excellent.
The property
Category
is a Multinode Treepicker which is setup to only have one node.Any ideas?
/Michaël
Hi
in Umbraco 7.6 Property Value Converters are on by default, which is why your MNTP is returning IEnumberable
but equally the default MNTP (
MultiNodeTreePicker2
) in Umbraco 7.6 stores UID values not Int values - so if you are using the new pickers you won't be able to get to the int values.If you want to still use the value converters (or if you using the new pickers) you can simplify the query a bit.
there is probably some clever xpath that does this really fast too - but the above will do it.
Hi Kevin,
was already thinking of something in that direction that had to be changed in newer version.
After some debugging I did find the UID that was stored but didn't know what it was untill now.
So, something new learned today thanks!
/Michaël
Any xpath pro that can give the xpath version for doing the same thing?
/Michaël
Xpath to loop the NestedContent? I don't know if you could make that work.
Damiaan,
ok then I will hold on the query way!
Thnx!
/Michaël
is working on a reply...