Copied to clipboard

Flag this post as spam?

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


  • James Feeney 26 posts 166 karma points
    Oct 10, 2013 @ 16:25
    James Feeney
    0

    Collecting multi-descendant children?

    I'm using this to collect Recipe Items inside a particular Repository.
    'collect' is the requested Repository name:

    rep = CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name == collect);

    Content        
      - > Homepage       
    - > Recipe Repository (named rep1)
        - > Recipe Item      - > Recipe Item
    - > Recipe Item
    - > Recipe Item

    - > Recipe Repository (named rep2)
     - > Recipe Item
     - > Recipe Item

    - > Recipe Repository (named rep3)
     - > Recipe Item
     - > Recipe Item

    - > Recipe Repository (named rep4)
     - > Recipe Item
     - > Recipe Item

    What do I have to change to select all the Recipe Items, regardless of which repository they are in?
    I've tried many things but none have worked. Any ideas? Thanks in advance!

     

  • Amir Khan 1289 posts 2746 karma points
    Oct 10, 2013 @ 18:06
    Amir Khan
    0

    Are they all the same document type? If so you can do something like this:

     

    // Get root node:
    var root = Model.AncestorOrSelf();

    // Get all descendants, filter by type:
    var nodes = root.Descendants("RecipeItem").Where("Visible");
  • James Feeney 26 posts 166 karma points
    Oct 14, 2013 @ 17:30
    James Feeney
    0

    Hi Amir, here is my code:

    They are all the same document type.

    I collect a querystring (the repository name). 'rep' is a set of items where their repository equals the query.

    But I need to collect all items if the query is "All'

    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
    if (!string.IsNullOrEmpty(Request.QueryString["RepositoryName"])) {
    string collect = String.Format("{0}", Request.QueryString["RepositoryName"]);
    DynamicNode rep = null;
    if (collect == "All") {
    rep = COLLECT ALL ITEMS SOMEHOW!
    } else {
    rep = CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name == collect);
    }

    I've tried your idea but I get error. 

    Thanks in advance.

     

  • Funka! 398 posts 661 karma points
    Oct 16, 2013 @ 04:34
    Funka!
    0

    Hi James,

    I was about to suggest what Amir posted, but if you are getting an error perhaps it is because you both have a mismatch in what "Model" represents... If not, could you please post the exact error message you're getting?

    My theory is that James appears to be using v6+ MVC razor, where "Model" is something of type RenderModel, while Amir appears to be posting pre-MVC code where "Model" is of type DynamicNode.

    (Oh, actually James I see you are using "CurrentPage" not Model, which I think is of type DynamicPublishedContent, but the problem still seems to be due to a mismatch in that first line of code.)

    How about this instead to fix...?

    if (collect == "All")
    {
        //COLLECT ALL ITEMS SOMEHOW!
        rep = CurrentModel.AncestorOrSelf().Descendants("recipeItem").Where("Visible"); // where recipeItem is the doc type alias
    }
    else
    ....
    

    Good luck!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies