Getting and displaying the 10 or x most recently updated pageson umbraco 12
I can get every published page recursively and display the ones of the documentTypes i want.
But im having trouble converting these pages (IPublishedContent into some sort of list or collection to be sorted by date.
I tried just appending the "content" var in the loop to an array, list, collection and ienumerable of IPublishedContent. But the list was always empty at runtime.
The code below works, but im not sure how i would make it display ordered by lastUpdateDate.
string[] docTypes = new string[] { "modPage"};
var results = Umbraco.AssignedContentItem.Children().Where(x => x.IsVisible());
void DisplayAllPagesOfType(IEnumerable<IPublishedContent> pages)
{
@foreach (var content in pages)
{
if (content != null)
{
bool isDoctypeMatch = false;
foreach (string docTyp in docTypes)
{
if (content.IsDocumentType(docTyp))
{
isDoctypeMatch = true;
break;
}
}
@if (isDoctypeMatch)
{
<div class="childBlock">
<a href="@content.Url()" class="indexBlockBtn btn" type="button"><b>@content.Name</b></a>
@if (content.Value<IEnumerable<IPublishedContent>>("gallery") != null)
{
var images = content.Value<IEnumerable<IPublishedContent>>("gallery");
if (images.Count() > 0)
{
<img class="img-fluid" src="@images.FirstOrDefault().GetCropUrl()" alt="@images.FirstOrDefault().Name" />
}
}
<div style="text-align:center">
@Html.Raw( content.Value("blurb"))
@Html.Raw(content.Value("fullDescription"))
</div>
</div>
}
}
var children = content.Children().Where(x => x.IsVisible());
DisplayAllPagesOfType(children);
}
}
@{
DisplayAllPagesOfType(results);
}
heres an updated script attempting to display by uiterating over a list of iPublishedContents. It executes without error, but only display a "0" in H1 for each returned page
string[] docTypes = new string[] { "modPage"};
var results = Umbraco.AssignedContentItem.Children().Where(x => x.IsVisible());
List<IPublishedContent> GetAllPagesOfType(IEnumerable < IPublishedContent > pages)
{
List<IPublishedContent> resultsList = new List<IPublishedContent>();
@foreach (IPublishedContent content in pages)
{
if (content != null)
{
foreach (string docTyp in docTypes)
{
if (content.IsDocumentType(docTyp))
{
resultsList.Append(content);
<h1>@resultsList.Count()</h1>
break;
}
}
}
var children = content.Children().Where(x => x.IsVisible());
GetAllPagesOfType(children);
}
return resultsList;
}
void DisplayPageBlock(IPublishedContent content)
{
var images = content.Value<IEnumerable<IPublishedContent>>("gallery");
<div class="childBlock">
<a href="@content.Url()" class="indexBlockBtn btn" type="button"><b>@content.Name</b></a>
@if (images.Count()>0)
{
<img class="img-fluid" src="@images.FirstOrDefault().GetCropUrl()" alt="@images.FirstOrDefault().Name" />
}
<div style="text-align:center">
@Html.Raw( content.Value("blurb"))
@Html.Raw(content.Value("fullDescription"))
</div>
</div>
}
void DisplayAllPageBlock()
{
foreach (IPublishedContent content in GetAllPagesOfType(results))
{
DisplayPageBlock(content);
}
}
I'm not completely sure what you are trying to achieve but, if I've understood correctly, you want an IEnumerable of IPublishedContent containing Children of the current page that are of a certain Document Type?
But if there is a specific DateTime property you have added to the Document Types that you want to order by, eg a custom Publish Date you've added then it would be:
I can use that to simplify some of my code, but the problem is that, that only fetches direct children. Not children of children and so on.
I have a simple site that hosts mods for video games. i have a custom document type "modPage". Every mod has it's own page. However they are not in the same directory. Each game is a directory or idnex page with many modPages as children.
So even if i run that code for eevery single directory i would end up with an ordered list from every directory, rather than one ordered masterlist of all modPages from every directory.
My posted code iterates through every single published page and displays every modpage. But has no way to order them. As each valid page was iterated i was trying to add it to a list or collection and then sort that masterlist by updateDate (which is default umbraco property, though i might be spelling it wrong here). But the list always returned empty for some reason.
Getting and displaying the 10 or x most recently updated pageson umbraco 12
I can get every published page recursively and display the ones of the documentTypes i want.
But im having trouble converting these pages (IPublishedContent into some sort of list or collection to be sorted by date.
I tried just appending the "content" var in the loop to an array, list, collection and ienumerable of IPublishedContent. But the list was always empty at runtime. The code below works, but im not sure how i would make it display ordered by lastUpdateDate.
heres an updated script attempting to display by uiterating over a list of iPublishedContents. It executes without error, but only display a "0" in H1 for each returned page
}
Hi Kodaxmax
I'm not completely sure what you are trying to achieve but, if I've understood correctly, you want an IEnumerable of IPublishedContent containing Children of the current page that are of a certain Document Type?
If so then the following:
But if there is a specific DateTime property you have added to the Document Types that you want to order by, eg a custom Publish Date you've added then it would be:
But let me know if it is something more complex than that and I will try to elaborate further!
regards
Marc
I can use that to simplify some of my code, but the problem is that, that only fetches direct children. Not children of children and so on.
I have a simple site that hosts mods for video games. i have a custom document type "modPage". Every mod has it's own page. However they are not in the same directory. Each game is a directory or idnex page with many modPages as children.
So even if i run that code for eevery single directory i would end up with an ordered list from every directory, rather than one ordered masterlist of all modPages from every directory.
My posted code iterates through every single published page and displays every modpage. But has no way to order them. As each valid page was iterated i was trying to add it to a list or collection and then sort that masterlist by updateDate (which is default umbraco property, though i might be spelling it wrong here). But the list always returned empty for some reason.
Hi Kodaxmax
How about using Descendants instead of Children?
it's essentially what you are trying to do?
regards
Marc
I belioeve it is, many thanks!
is working on a reply...