Copied to clipboard

Flag this post as spam?

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


  • Kim Andersen 1447 posts 2196 karma points MVP
    Nov 18, 2014 @ 21:43
    Kim Andersen
    0

    Razor: How to get ancestor where boolean set to true in Umbraco 7

    Hi there

    I'm working on a Umbraco 7 solution. On my pages the editors can select some related pages using a MultiNodeTree Picker. Theese are renderes using a helper in my Razor partial, and everything is fine. That code look like the following:

    @renderSharedContent(CurrentPage.selectedPages)
    
    ...
    ...
    ...
    
    @helper renderSharedContent(String listOfIds)
    {
        var boxList = listOfIds.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
    
        // Convert box list to Typed Content
        var boxCollection = Umbraco.Content(boxList);
    
        // Looping through selected shared content items as IPublishedContent (Typed Content)
        foreach (IPublishedContent item in boxCollection)
        {
            //Calling partial, not cached
            @Html.Partial(@item.DocumentTypeAlias, item)
        }
    }
    

    So what I need to implement now is the ability to inherit the selected pages to the underlying pages.

    This means that on each page I have a true/false property called "Inherit selected pages". If this checkbox is set to true the selected pages must be shown on the underlying pages.

    So let's say that I have the following content.

    - Homepage
    -- Section 1
    --- Subpage 1.1
    ---- SubSubpage 1.1.1
    ---- SubSubPage 1.1.2
    --- Subpage 1.2
    --- Subpage 1.3
    -- Section 2
    etc.
    

    On Section 1 I have selected two pages and checked the "Inherit selected pages". This means that when I'm standing on any of the pages below this page they should render the selected pages from the ancestor page.

    It's possible to both show inherited pages, and also render pages selected on the single page. So if I've selected some additional pages on the page "SubSubpage 1.1.2", the page would both show the inherited pages and the "local" selected pages below.

    But I can't find out how to find the ancestor node that have the checkbox ("inheritSelectedPages") set to true, and then use the content from that node in the "selectedPages"-MNTP. I guess that if I had the ancestor, I could render my pages like the following:

    var inheritNode = [what to write here??]
    @renderSharedContent(inheritNode.selectedPages)
    @renderSharedContent(CurrentPage.selectedPages)
    

    Btw. if both the parent, the parents parent etc. have checked the "inheritSelectedBoxes" it should only be the pages from the parent that should be inherited. The first node with the "inheritSelectedPages" in the "way up" the nodes.

    I hope that my question makes sense, and hat someone could give me some pointers on how to achieve this :)

    Thank you very much in advance.

    /Kim A

  • Vasia Vasia 49 posts 241 karma points
    Nov 18, 2014 @ 21:58
    Vasia Vasia
    0

    Hi Kim,

    To get ancestor you can use the following methods:

    AncestorsOrSelf() AncestorsOrSelf(int level) AncestorsOrSelf(string nodeTypeAlias)
    After you have found out the ancestor you can see its properties.
    Hope this helps.
    Oleg
  • Kim Andersen 1447 posts 2196 karma points MVP
    Nov 18, 2014 @ 22:08
    Kim Andersen
    0

    Hi Oleg

    Thanks four your answer.

    I'm aware of the AncestorsOrSelf.

    I forgot to mention in my first post, that I have tried some different suff to achieve this. For example I have tried something like the below:

    var inheritNode = CurrentPage.AncestorsOrSelf().Where("inheritSelectedPages").Count() > 0;
    

    But the above seems to give me a true/false in return, so it's not what I'm after.

    Any other ideas are welcome :)

    /Kim A

  • Vasia Vasia 49 posts 241 karma points
    Nov 18, 2014 @ 22:31
    Vasia Vasia
    0

    Hi Kim,

    If I correct understand your issue the following may help:

    public IPublishedContent GetInheritedAncestor(IPublishedContent content)

        {

            if (content.Parent != null)

            {

                var isInherited = content.Parent.GetPropertyValue("inheritSelectedPages");

                if (isInherited)

                {

                    return GetInheritedAncestor(content);   

                }

                    return content.Parent;

            }

     

            return null;

        }

     

    Thanks,

    Oleg

  • Vasia Vasia 49 posts 241 karma points
    Nov 18, 2014 @ 22:51
    Vasia Vasia
    2

    Or if you need all parents with inherited property = true

    Ancestors().Where(x => x.GetPropertyValue<Boolean>("inheritSelectedPages"));

     

    Oleg

  • Kim Andersen 1447 posts 2196 karma points MVP
    Nov 19, 2014 @ 08:57
    Kim Andersen
    0

    Hi again Oleg

    Sorry for my stupidity, but I'm not quite sure how I use your snippet. I come from a XSLT background (I can't write C#) so in XSLT I would just write something like:

    $currentPage/ancestor-or-self::*[@isDoc and inheritSelectedPages='1' and @id!=$currentPage/@id]
    

    Something like the above would give me the node of the closest ancestor with the true/false-property checked.

    I'm not sure how I could achieve this with the above code you provided me.

    There's not a way to achieve something like that, so I could use it like below?

    var inheritNode = [what to write here??]
    @renderSharedContent(inheritNode.selectedPages)
    @renderSharedContent(CurrentPage.selectedPages) //This line already works perfectly
    

    Maybe I'm just dreaming of doing this "the easy way" and to grab the node into the inheritNode variable.

    But if that's not possible how should I implement your code in my Partial?

    Maybe it's some stupid question, sorry :-/

    /Kim A

Please Sign in or register to post replies

Write your reply to:

Draft