Copied to clipboard

Flag this post as spam?

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


  • Matthew 138 posts 201 karma points
    Apr 13, 2014 @ 02:45
    Matthew
    0

    Retrieve children of a lower branch and of a different branch

    Two different issues, for a company that services multiple sectors by performing projects.  Site visitors may be interested in what the company has done in a particular sector, or may be interested in specific projects, so the presentation is by sectors or by projects.

    Umbraco 7.1.1

    Site stucture is like this:

    Home
    - Sectors
    - - Sector1
    - - Sector2
    - Projects
    - - Project1
    - - Project2
    - - Project3

    From the Home page, I want to be able to feature the individual sectors (and possibly a project or two).

    From the Sectors page, I want to be able to list projects that are relevant to a sector.

    From the Projects page, I want to list all the projects.

    So on the Home page, there would be:

    Home
    Sector1 | Sector2

    From the Sector1 page, there would be:

    Sector1
    Project2  | Project3

    From the Projects page, there would be:

    Projects
    Project1 | Project2 | Project3

    I'm having a heck of a time trying to figure out how to retrieve the children of a specific node, one level removed.  There must be a way to find it by name or title or something.  I've seen a lot of similar stuff but not quite the same (or maybe I just don't get what I'm seeing).  And I've seen a fair amount of this but with xslt (before my time).

    Seems like if I can target a specific node, I'm golden but I've been poking at it for quite a bit and not getting it.

    Any clue how to do this?

    Thanks much.

  • Matthew 138 posts 201 karma points
    Apr 13, 2014 @ 02:57
    Matthew
    0

    I should also say that for flexibility, I'm adopting Kevin's LocalGov model which uses generic section doctypes.  'Sectors' and 'Projects' both have the same SectionHomepage doctype.  The idea is additional sections can be added easily any time. 

  • Kevin Jump 2343 posts 14890 karma points MVP 8x c-trib
    Apr 13, 2014 @ 14:15
    Kevin Jump
    0

    Hi

    There are a couple of ways of approaching this. (these examples are for Partial views, it's slightly different doing macroscripts)

    the 'simple' way would be to go to the root of the site

    var root = Model.Content.AncestorOrSelf(1); 
    

    and then get all lets say Project pages with a certain value set (maybe the sector they relate to) which would be something like

    foreach (var node in root.DescendantsOrSelf()
          .Where(x => x.DocumentTypeAlias == "protjectdoctype" && x.GetPropertyValue("Sector") == "sector1")
    {
    ...
    }
    

    That gets you all projectdoctype elements across the site, that would also have sector set to "sector1".

    this is the query the whole lot way - and it depends on things like how many projects and how deep the site is - I would at the very least start from the project node so you're not searching the whole site, one way to do this for the editors is to put a picker on your root node, allowing people to pick the node they want the search to start at. then you can go to the root and get the node id.

    var projectRootID = root.GetPropertyValue("projectRoot").
    var projectRootNode = Model.GetById(projectRootID)
    
    foreach(..... in projectRootNode)
    

    either way I would cache this macro on a live site because it's not going to be the fastest part of the site.

    if you have loads of project nodes you might want to consider using Examine (the search index) to build an index of projects and then use that to search for projects relating to a sector, and then display them - this will be the fastest way but the code will be slightly more complex.

    the code above is using Lamda expressions but you can use Xpath using things like ContentAtXPath("//project")

    hope that at least gives some pointers...

    some of the code has changed but the 8 Razor walkthroughts on the umbraco blog are still a really good place to go for all the things you can do with Razor - part 4 has lots of good stuff on quering

    warning everything in here i've just typed into the response, you will want to check it compiles/works etc...

  • Matthew 138 posts 201 karma points
    Apr 13, 2014 @ 20:27
    Matthew
    0

    Thanks Kevin, I'll get to work with that as soon as I get back in the office.  I've read the razor walk throughs a number of times and get the basics but some of the finer points are not spelled out rudimentarily enough me.  Being on the right track really helps, thanks for steering me there.

  • Matthew 138 posts 201 karma points
    Apr 14, 2014 @ 23:06
    Matthew
    0

    I've been working with this and had some success but I don't think I've hit on the elegant solution yet.  This works but requires that I have the property 'isSector' on the SectionHomepage doctype, an extra management steps for editors. 

    @{
        var root = Model.Content.AncestorOrSelf(1);
    }
       @foreach (var node in root.DescendantsOrSelf().Where(x => x.DocumentTypeAlias == "SectionHomepage"))
        {
            if(node.HasValue("isSector"))
            {
                <p>@node.GetPropertyValue("title")</p>
            }
    }

    I think Umbraco should be able to figure out what's under the Sectors content node by checking the 'title' property is 'Sectors':

    .Where(x => x.GetPropertyValue("title") == "Sectors")

    So far though, I have been unable to construct that.

    I tried it by adding another var and running the foreach on that but it doesn't return them either:

    var root = Model.Content.AncestorOrSelf(1);
    var
    sectionRoot = root.DescendantsOrSelf().Where(x => x.GetPropertyValue("title") == "Sectors");
    @foreach (var node in sectionRoot)

    BTW, I tried using the suggestion with Model.GetById() but it complains about 'System.Web.Mvc.ModelBinderAttribute' does not contain a definition for 'GetById'.  I tried adding in the @using Umbraco.Core/Models/Services but that didn't cheer it up.

    This seems pretty basic, to be able to pick a node by it's name or title, and I've seen a lot of posts about it in the xslt forum but not so much in razor.

    Any more ideas?

     

  • Matthew 138 posts 201 karma points
    Apr 14, 2014 @ 23:11
    Matthew
    0

    Just to refresh, the main goal is from the Home page, I want to be able to list the individual sectors:

    Home
    - Sectors
    - - Sector1
    - - Sector2
    - - Sector3

    So rendered in the browser:

    Homepage content
    Sector1 | Sector2 | Sector3

  • Kevin Jump 2343 posts 14890 karma points MVP 8x c-trib
    Apr 15, 2014 @ 10:21
    Kevin Jump
    1

    Hi Matthew -

    you should be able to do string comparisons on the title so x.GetPropertyValue("title").StartsWith("Sectors"), It's possible to construct the queries in a number of ways to put almost any criteria in there - however I would suggest it is starting to get messy, I Would say from what you describe I would create a sector doctype in its own right.

    Using the StarterKit : If you still need the Section Homepage template and Sectors then I would say crate a new child DocType of SectionHomepage called SectorHomepage and use that for sectors - it inherits all the properties and isn't something you have to add to. then in the code just find the sector Doctypes and use them for this - the code for this is much simpler (it's then same as the macro that generates the links on the homepage.

    editors could choose to create section homepages - and have them list the projects.

    what you might also want to look at is how projects are assigned to sectors in v6 with uComponents you could use the XPATH Dropdown list - so within the project node the user gets a list of sections they can select from - i'm not sure if there is a v7 equivalent yet.

    @Model.GetByID is @Model.NodeById - my bag - that's what happens when you just type the stuff in to a comments box

  • Matthew 138 posts 201 karma points
    Apr 15, 2014 @ 17:56
    Matthew
    0

    Well, that's sensible.  I was kind of obsessing on retaining the flexibility of generic sections in your package but now you mention it, I think I attempted to add too many variations on your theme, which as you say just gets messy.  I'll still try the additional query criteria you suggest though, I did not realise that was possible.  And thanks for clarifying the ...ById thing, I think I should have been able to sort that one out myself but...  apparently not.

    Have a great evening!

Please Sign in or register to post replies

Write your reply to:

Draft