Copied to clipboard

Flag this post as spam?

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


  • Elliott Brown 41 posts 211 karma points
    Jul 17, 2017 @ 08:46
    Elliott Brown
    0

    Get content list by document type

    Hi and thank you in advance!

    I have a situation where all all of my site videos are located under different branches of the content tree. I want to be able to get all of them and display them on a list page. After trawling through the forum I have arrived at the following code. However, it returns null.

    Any ideas?

    var allVideos = Model.Content.AncestorOrSelf().Descendants("videoDocType");
    <ul>
    @foreach (var publishedVideo in allVideos)
    {
         <li>@publishedVideo.Name</li>
    }
    </ul>
    
  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jul 17, 2017 @ 08:52
    Alex Skrypnyk
    0

    Hi Elliott

    Is "videoDocType" name of nodes doctype of video items?

    Model.Content.AncestorOrSelf().Descendants("videoDocType") - returns videoDocType items from current tree, you have to look at all tress if you have few.

    You need something like that:

    var rootItems = Model.Content.Site().Siblings();
    var allVideos = rootItems.Select(x => x.Descendants("videoDocType")).ToList();
    

    Thanks,

    Alex

  • Elliott Brown 41 posts 211 karma points
    Jul 17, 2017 @ 09:04
    Elliott Brown
    0

    Thanks Alex,

    videoDocType is the document type alias. This is the document type used to store each videos data.

    Unfortunately I'm getting

    'IPublishedContent' does not contain a definition for 'Site' and no extension method accepting a first argument of type could be found (are you missing a using directive or an assembly reference?)

    Your logic makes sense though. I need to check every content tree, not just one. As you can probably tell, I'm no expert! ;) - so it's all very helpful! Thanks

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jul 17, 2017 @ 09:08
    Alex Skrypnyk
    0

    Probably this code will work:

    var allVideos = Model.Content.AncestorOrSelf().Siblings().SelectMany(x => x.Descendants("videoDocType"));
    
  • Elliott Brown 41 posts 211 karma points
    Jul 17, 2017 @ 09:18
    Elliott Brown
    0

    That code didn't but this does. I removed the .Site()

    definitely luck rather than judgement... any reason not to do it this way?

    var rootItems = Model.Content.Siblings();
    var allVideos = rootItems.SelectMany(x => x.Descendants("clicklearnVideoPage")).ToList();
    
  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jul 17, 2017 @ 09:20
    Alex Skrypnyk
    100
    var allVideos = Model.Content.AncestorOrSelf().AsEnumerableOfOne().Union(Model.Content.AncestorOrSelf().Siblings()).SelectMany(x => x.Descendants("videoDocType"));
    
  • Elliott Brown 41 posts 211 karma points
    Jul 17, 2017 @ 09:29
    Elliott Brown
    0

    Great! is this longer version a better solution because the pitfalls of the descendants method? - either way, solution found! Thanks!

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jul 17, 2017 @ 09:31
    Alex Skrypnyk
    0

    Descendants() method is a bad idea to use because of speed, if you will have some problems with the performance of your page - then please ask, we will find a way to fix performance.

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Jul 17, 2017 @ 08:53
    Alex Skrypnyk
    0

    Also, be aware that "Descendants" method is pretty slow if you have a lot of content items it will work slow.

    Read this article how to avoid it - https://our.umbraco.org/documentation/reference/Common-Pitfalls/

Please Sign in or register to post replies

Write your reply to:

Draft