Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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.
var allVideos = Model.Content.AncestorOrSelf().Descendants("videoDocType");
@foreach (var publishedVideo in allVideos)
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();
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
Probably this code will work:
var allVideos = Model.Content.AncestorOrSelf().Siblings().SelectMany(x => x.Descendants("videoDocType"));
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();
var allVideos = Model.Content.AncestorOrSelf().AsEnumerableOfOne().Union(Model.Content.AncestorOrSelf().Siblings()).SelectMany(x => x.Descendants("videoDocType"));
Great! is this longer version a better solution because the pitfalls of the descendants method? - either way, solution found! Thanks!
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.
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/
Write your reply to:
Image will be uploaded when post is submitted