ms.GetPagedChildren(item.Id, 0, 100, out long total);
this would get the first 100 items (nothing but performance stopping you upping the pageSize variable)
a more comprehensive way to get everything
const int pageSize = 500;
var page = 0;
var total = long.MaxValue;
while(page * pageSize < total)
{
var children = ms.GetPagedChildren(item.Id, page++, pageSize, out total);
// do stuff wit the child items
}
Also GetPagedDescendants will get all items down the tree.
Get IMedia children in Umbraco 8
Hi,
I'm trying to get all the child folders of a media folder in Umbraco 8. I'm using the
MediaService
to get a root media item (folder) like this:Now I can use the
MediaService
to check if theIMedia
item has children using the.HasChildren()
method.Is there any way to get all the child folders as
IEnumerable<IMedia>
from thechallengesRootFolder
in the code snippet?Hi,
you can get the child items through
this would get the first 100 items (nothing but performance stopping you upping the pageSize variable)
a more comprehensive way to get everything
Also GetPagedDescendants will get all items down the tree.
Update: Just getting folders is a little more complex, but you can see how Umbraco does it in the source for the media controller https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Web/Editors/MediaController.cs#L196
Thanks this worked! I managed to get only folders by simply adding a
Where
to check theContentType.Alias
in the while loop. Like this:is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.