How can I find all nodes with a certain property value?
Hello!
I know how to find values of properties for a node, but now I need to go in the other direction: find nodes based on value of a property. I see some results for this questions around the web referring to a thing called uQuery, but I've also read that this is old and should no longer be used.
Basically, we have document types with a property called "projectList". I want to be able to get a list of all nodes whose value for "projectList" includes some string. How can I query for node names based on property value?
Here's a quick example we use to grab blog posts by categories.
var startNode = Umbraco.Content(1234);
var selection = startNode.Descendants("BlogPost").Where("Visible").Where("@postCategories.ToString().Contains(@0)", "name of category");
foreach(var post in selection) {
// render the posts here
}
Not the best practice to hard code the start node so we have a settings tab with a content picker on the homepage to pick the blog section for each site.
what we always do for this kind of queries is creating extension methods on the IPublishedContent class ( which is the type each content node has ).
So what you could do is creating a static class called RazorExtensions and have a method called CheckProject like this:
public static class RazorExtensions
{
public static bool CheckProject(this IPublishedContent node, string project)
{
if (!string.IsNullOrEmpty(project))
{
var projects = node.GetPropertyValue<IEnumerable<string>>("projectList");
return projects.Any(x => x == project);
}
return false;
}
}
What this method does is checking the current IPublishedContent node from the list and check if the property projectList has any given project ( which is the second attribute from the method.
It returns true if this is the case else false.
Then you can call this method where you need it ( make sure to reference to this class with using ).
I understand the overall concept here, but I'm not sure how to wire up CheckProject so that it can be part of IPublishedContent. I've created an extension method in my project; however, when I try to add this to my surface controller (because this is not going on a Razor view, but into a surface controller that returns data to a plugin), I am told, "'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'CheckProject'."
Hello. Yeah, even when I make sure that is there, I still get the error. Interestingly, if I have my extensions class in another project in this solution, that project builds fine, but in the line below, IPublishedContent doesn't have any highlighting or intellisense.
public static bool CheckProject(this IPublishedContent node, string project)
How can I find all nodes with a certain property value?
Hello!
I know how to find values of properties for a node, but now I need to go in the other direction: find nodes based on value of a property. I see some results for this questions around the web referring to a thing called uQuery, but I've also read that this is old and should no longer be used.
Basically, we have document types with a property called "projectList". I want to be able to get a list of all nodes whose value for "projectList" includes some string. How can I query for node names based on property value?
Thanks in advance!
Hi Jeremy,
Here's a quick example we use to grab blog posts by categories.
var startNode = Umbraco.Content(1234); var selection = startNode.Descendants("BlogPost").Where("Visible").Where("@postCategories.ToString().Contains(@0)", "name of category");
foreach(var post in selection) { // render the posts here }
Not the best practice to hard code the start node so we have a settings tab with a content picker on the homepage to pick the blog section for each site.
Hope this helps!
Amir
Hi Jeremy,
what we always do for this kind of queries is creating extension methods on the IPublishedContent class ( which is the type each content node has ).
So what you could do is creating a static class called
RazorExtensions
and have a method calledCheckProject
like this:What this method does is checking the current IPublishedContent node from the list and check if the property
projectList
has any given project ( which is the second attribute from the method.It returns true if this is the case else false.
Then you can call this method where you need it ( make sure to reference to this class with using ).
Like for instance:
Which will give you a IEnumerable list of content nodes which has
project1
in theprojectList
property.*This is manualy typed so can contain typos!
Hope this helps.
/Michaël
Michaël,
I understand the overall concept here, but I'm not sure how to wire up CheckProject so that it can be part of IPublishedContent. I've created an extension method in my project; however, when I try to add this to my surface controller (because this is not going on a Razor view, but into a surface controller that returns data to a plugin), I am told, "'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'CheckProject'."
What am I missing?
Thanks for bearing with me!
Jeremy
Hi Jeremy,
like I said, you have to make sure that you have added the reference to your extensions class with using.
Hope this helps.
/Michaël
Hello. Yeah, even when I make sure that is there, I still get the error. Interestingly, if I have my extensions class in another project in this solution, that project builds fine, but in the line below, IPublishedContent doesn't have any highlighting or intellisense.
I'll keep scratching my head on this one.
is working on a reply...