Copied to clipboard

Flag this post as spam?

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


  • David W. 159 posts 284 karma points c-trib
    Feb 05, 2012 @ 20:06
    David W.
    0

    AncestorOrSelf by document type

    Hello,

    Using 4.7 I know it's possible to have something like Model.AncestorOrSelf(level). But what would be the best way to get the closest ancestor(or self) of a perticular documenttype?

    /D

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Feb 05, 2012 @ 20:12
    Sebastiaan Janssen
    0

    That would be by including it in a Where statement:

    @Model.AncestorOrSelf.Where("NodeTypeAlias == @0", "yourDocumentTypeAlias")

  • David W. 159 posts 284 karma points c-trib
    Feb 05, 2012 @ 20:23
    David W.
    0

    Hm, not getting the result I'm hoping for. I tried to replace a bit of code out of a razor-snippet. I was hoping the two would have the same result, but it appears as the second one renders zero children

    var level String.IsNullOrEmpty(Parameter.Levelint.Parse(Parameter.Level)
      var ulClass String.IsNullOrEmpty(Parameter.UlClass"" String.Format(" class=\"{0}\""Parameter.UlClass)
      //var parent @Model.AncestorOrSelf(level);
      var parent @Model.AncestorOrSelf.Where("NodeTypeAlias == @0""Start");
      
      if (parent != null{
        <ul@Html.Raw(ulClass)>
        @foreach (var item in parent.Children.Where("Visible"){
          var selected Array.IndexOf(Model.Path.Split(',')item.Id.ToString()>= " class=\"selected\"" "";
          <li@Html.Raw(selected)>
            <href="@item.Url">@item.Name</a>
          </li>
          }
        </ul>
      }

  • Ulrich Wagner Gade Ebsen 127 posts 277 karma points
    Feb 05, 2012 @ 20:28
    Ulrich Wagner Gade Ebsen
    0

    @Model.AncestorsOrSelf("name-of-doctype").Last()

  • David W. 159 posts 284 karma points c-trib
    Feb 05, 2012 @ 20:39
    David W.
    0

    Hm, still the same result.  

    Using var parent @Model.AncestorOrSelf(level)results in  @parent.Children.Count() == 11

    while

    var parent @Model.AncestorOrSelf("Start").Last()results in  @parent.Children.Count() == 0;

  • Ulrich Wagner Gade Ebsen 127 posts 277 karma points
    Feb 05, 2012 @ 20:47
    Ulrich Wagner Gade Ebsen
    0

    Notice the extra "s" in ancestorsOrSelf... it's a collection.. ;)

  • David W. 159 posts 284 karma points c-trib
    Feb 05, 2012 @ 20:50
    David W.
    0

    Brilliant. Thanks!

  • Baldy 20 posts 40 karma points
    Oct 10, 2013 @ 20:05
    Baldy
    0

    Where are these APIs documented?

    I have paid for umbraco.tv, but i cannot find anything on the site that lets me work these things out for myself.

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 10, 2013 @ 20:27
  • Baldy 20 posts 40 karma points
    Oct 11, 2013 @ 10:41
    Baldy
    0

    Dennis, many thanks.

    The last one is particularly useful as a point of reference for MVC.

    I resorted to using the Object Browser to figure out what @CurrentPage had to offer (an instance of DynamicPublishedContent).

    I also came across these cheat sheets, which were a huge help...

    http://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets

  • Funka! 398 posts 661 karma points
    Oct 11, 2013 @ 21:06
    Funka!
    0

    Running visual studio on the umbraco source alongside any kind of umbraco development and using the Object Browser as you did is really the only way to be effective in my opinion. So many nuances which are just easier to look at the source than digging or searching around the net. The cheat sheet also is a great reference for getting started (I have mine printed out and always within reach myself) but not totally comprehensive, not quite like the Object Browser is at least. Best of luck to you!

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 12, 2013 @ 03:17
    Fuji Kusaka
    0

    Hi David,

    The other closest way of getting something like Model.AncestorOrSelf(level) would be

    List<DynamicNode> content = @Model.AncestorOrSelf(2).Descendants("DocumentType").Items;

    then something like

    if(content.Count() > 0 ){
    foreach(var s in content){
    @s.Name or @s.Url
    }
    }
Please Sign in or register to post replies

Write your reply to:

Draft