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>
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
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.
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?
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:
Thanks,
Alex
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
Probably this code will work:
That code didn't but this does. I removed the .Site()
definitely luck rather than judgement... any reason not to do it this way?
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/
is working on a reply...