Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jeremy Coulson 43 posts 125 karma points
    Mar 05, 2018 @ 19:59
    Jeremy Coulson
    0

    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!

  • Amir Khan 990 posts 1988 karma points
    Mar 05, 2018 @ 20:06
    Amir Khan
    0

    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

  • Michaël Vanbrabandt 849 posts 3292 karma points c-trib
    Mar 05, 2018 @ 20:20
    Michaël Vanbrabandt
    0

    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 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 ).

    Like for instance:

    Model.Content.Children.Where(x => x.CheckProject("project1"));
    

    Which will give you a IEnumerable list of content nodes which has project1 in the projectList property.

    *This is manualy typed so can contain typos!

    Hope this helps.

    /Michaël

  • Jeremy Coulson 43 posts 125 karma points
    Mar 05, 2018 @ 21:49
    Jeremy Coulson
    0

    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

  • Michaël Vanbrabandt 849 posts 3292 karma points c-trib
    Mar 06, 2018 @ 11:40
    Michaël Vanbrabandt
    0

    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

  • Jeremy Coulson 43 posts 125 karma points
    Mar 07, 2018 @ 15:00
    Jeremy Coulson
    0

    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)
    

    I'll keep scratching my head on this one.

Please Sign in or register to post replies

Write your reply to:

Draft