Hi,
I am trying to work out using the IMediaService how to get the child items of a folder.
But can’t see a method to do this.
As I want to iterate all the images on the site and check to make sure a specific custom field been set.
Ta
Darren
Hi,
I am trying to work out using the IMediaService how to get the child items of a folder.
But can’t see a method to do this.
As I want to iterate all the images on the site and check to make sure a specific custom field been set.
Ta
Darren
You need to do this:
var children = _mediaService.GetPagedChildren(folderId, 0, 100, out long total);
Justin
Do you need to use the MediaService for that, rather than the PublishedContentCache to save hitting the database?
var folder = _publishedContentQuery.Media(folderGuid);
// Verify it's actually a folder
if (folder != null && folder.ContentType.Alias == Umbraco.Cms.Core.Constants.Conventions.MediaTypes.Folder)
{
// Get all files or sub-folders inside this folder
foreach (var item in folder.Children())
{
var name = item.Name;
var isNestedFolder = item.ContentType.Alias == Umbraco.Cms.Core.Constants.Conventions.MediaTypes.Folder;
}
}
I wonder as well if one of the mediaManager packages could benefit from this sort of query?
in no particular order… and sure there are others…
Umbraco Media Manager | Umbraco Marketplace
uMediaOps - Media Management Suite for Umbraco | Umbraco Marketplace
Here my solution
using Microsoft.AspNetCore.Authorization;using Microsoft.AspNetCore.Mvc;using System.Diagnostics.Tracing;using Umbraco.Cms.Core.Models;using Umbraco.Cms.Core.Models.PublishedContent;using Umbraco.Cms.Core.Services;using Umbraco.Cms.Infrastructure.Persistence.Querying;using Umbraco.Cms.Infrastructure.Scoping;using Umbraco.Cms.Web.Common;using Umbraco.Cms.Web.Common.Authorization;using Umbraco.Cms.Web.Common.Filters;using Umbraco.Forms.Core.Interfaces;using static Umbraco.Cms.Core.Constants.HttpContext;
namespace UmbracoCMS.App_Code.ALtCheck{[Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)][DisableBrowserCache]// https://github.com/umbraco/Umbraco-CMS/blob/v9/contrib/src/Umbraco.Web.Common/Controllers/UmbracoAuthorizedController.cs// https://github.com/umbraco/Umbraco-CMS/blob/v10/main/src/Umbraco.Web.Common/Controllers/UmbracoAuthorizedController.cspublic class ALTCheckController : Microsoft.AspNetCore.Mvc.Controller{IMediaService _mediaService;IContentService _contentService;private readonly UmbracoHelper _umbracoHelper;List allowedTypes = new List();List files = new List();public ALTCheckController(IMediaService mediaService, IContentService contentService, UmbracoHelper umbracoHelper){_mediaService = mediaService;_contentService = contentService;_umbracoHelper = umbracoHelper;}
[Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)]
[DisableBrowserCache]
[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
[Route("umbraco/backoffice/plugins/ALtCheck/getContent")]
[HttpGet]
public JsonResult getContent()
{
IEnumerable<IMedia> root = _mediaService.GetRootMedia();
/*
* umbracoMediaArticle
* umbracoMediaAudio
* ImageCarousel
* FolderCarousel (*)
* imageEvent
* FolderEvents (*)
* File
* Folder (*)
* Image
* ImageCarousel1
* FolderNews (*)
* SecureFile
* SecureFolder (*)
* imageStaff
* FolderCarousel1 (*)
* umbracoMediaVectorGraphics
* umbracoMediaVideo
*/
allowedTypes.Add("FolderCarousel");
allowedTypes.Add("FolderEvents");
allowedTypes.Add("Folder");
allowedTypes.Add("FolderNews");
allowedTypes.Add("SecureFolder");
allowedTypes.Add("FolderCarousel1");
foreach (IMedia media in root) {
loopNode(media);
}
return Json(files);
}
private void loopNode(IMedia media) {
Boolean processed = false;
Boolean altText = true;
foreach (string s in allowedTypes)
{
if (media.ContentType.Alias == s)
{
Boolean hasChildren = _mediaService.HasChildren(media.Id);
if (hasChildren)
{
var mediaItem = _umbracoHelper.Media(media.Id);
if (mediaItem != null) {
IEnumerable<IPublishedContent> childItems = mediaItem.Children;
foreach (IPublishedContent item in childItems) {
IMedia publishedMedia= _mediaService.GetById(item.Id);
loopNode(publishedMedia);
processed = true;
}
}
}
}
}
if (!processed)
{
if (media.HasProperty("altText")){
var alttext = media.GetValue<string>("altText");
try
{
if (alttext == null)
{
altText = false;
}
else
{
if (alttext.Length == 0)
{
altText = false;
}
}
}
catch (Exception ex)
{
altText = false;
}
if (!altText)
{
MediaFileMissingAltText newFile = new MediaFileMissingAltText();
newFile.FileName = media.Name;
newFile.id = media.Id.ToString();
files.Add(newFile);
}
}
}
}
} }
Dose what I want not exactly quick but dose it!!!
Prob not quick as you are hitting the DB.. try the IPublishedContentCache (as you don’t seem to be doing any updates to the media items you find..)
And if that is still slow.. go over to using examine and see if that improves performance?
(even if you do need to do updates.. you can use the IPublishedContentCache, Examine first.. and then use the ID to fetch that explicit media item with the mediaService and update it, only one hit per update to the DB.. instead of a read for every single media item)
Also with the later two you can be much more targeted and do a get me the only the media items that match your missing condition (rather than enumerating over the entire media items..)
MediaService.GetChildren() does allow for a filter, but AFAIK it’s still restricted to umbraco core properties and not custom ones..
IEnumerable<IMedia> GetPagedChildren(int id, long pageIndex, int pageSize, out long totalRecords, IQuery<IMedia>? filter = null, Ordering? ordering = null);