Copied to clipboard

Flag this post as spam?

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


  • Mohammad Khan 23 posts 81 karma points
    Nov 26, 2014 @ 18:01
    Mohammad Khan
    0

    Issue with iterating using DescendantsOrSelf

    I just recently upgraded Umbraco from 4.7.2 to 7.1.9 and now in the process of slowly converting all legacy macroscripts to partial view macros. I have come across a few issues when using DescendantsOrSelf to iterate through nodes.

    I have a macro that generates the side menu for my site (intranet). With version 4 the macro worked as expected on the whole site displaying the appropriate menu on the homepage and different side menu's on the child pages.

    After the upgrade the below condition:

    var model = GetParentSideMenu(CurrentPage);
    
    if (CurrentPage.AncestorsOrSelf("umbSomePageType").Where("Visible").First().HasValue("PageName") && CurrentPage.AncestorsOrSelf("umbSomePageType").Where("Visible").First().Id != model.Id)
    { ... } @functions { public dynamic GetParentSideMenu(dynamic model) { if (model.Level > 1) { do { if (model.umbSideMenuLinks.Count() > 0) { return model; } if (model.Level > 1) { model = model.Up(); } else { break; } } while (model.Up() != null); return model; } else return model; } }

    Generates the following error when rendered on the homepage:

    System.InvalidOperationException {"Sequence contains no elements"}

    Inner Exception is null

    The understanding here is that the page being rendered is not "umbSomePageType" so this condition should be false and move on but instead it throws the above exception.

    The macro works fine when rendered on a "umbSomePageType" page but as the user is allowed to have further sub pages of a another type under "umbSomePageType" I have to manually check the "DocumentTypeAlias" and make sure the other if statements checking for that type of sub page are not executed because I get the same error as above.

    Another issue I am facing is this doesn't return an iterable collection when it used to before in v4:

    @if (model.DescendantsOrSelf("umbSideMenuLinks").Where("Visible").Count() > 0)
    {
        foreach (var item in model.DescendantsOrSelf("umbSideMenuLinks").Where("Visible").First().Children)
        { ... } .... }

    The if condition returns true but the foreach is unable to get any elements to iterate through.

    Any help here will be greatly appreciated.

    Thank You.

  • Mohammad Khan 23 posts 81 karma points
    Dec 30, 2014 @ 16:14
    Mohammad Khan
    100

    I was able to find a solution for this problem. One is to change First() to FirstOrDefault() considering that CurrentPage is a dynamic object. Second is that your content must be in the correct order for the DescendantsOrSelf to find what you want it to. In my case I had my root node than I had a sub folder with sub pages including the above mentioned doctype which was present but had no content. I wanted to render the parent doctypes content. After sorting it in the correct order it solved the problem.

Please Sign in or register to post replies

Write your reply to:

Draft