Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Craig100 1136 posts 2523 karma points c-trib
    May 24, 2020 @ 12:08
    Craig100
    0

    Check if Media Folder exists from a core class

    Umb 8.6.1

    Upgrading V7 code to V8

    In a class that gets images from an API and stuffs them into appropriate folders in Media I need to check the folder exists and if not, create it. As we can't now get Children easily and it doesn't look like I have IPublishedContent available either I'm looking for some help with syntax converting this (which works in V7):-

    var propertiesRootFolder = mediaService.GetRootMedia().FirstOrDefault(m => m.Name.InvariantEquals("Properties"));
    IMedia propertyFolder = propertiesRootFolder?.Children().FirstOrDefault(c => c.Name.InvariantEquals(propertyFolderName));
    

    To V8.6.1. What I've got so far is this:-

    var propertiesRootFolder = mediaService.GetRootMedia().FirstOrDefault(m => m.Name.InvariantEquals("Properties"));
    IMedia propertyFolder = propertiesRootFolder?.GetPagedChildren(1, 0, int.MaxValue, out var folderCount).Where(x => x.ContentType.Alias == Constants.Conventions.MediaTypes.Folder).FirstOrDefault(c => c.Name.InvariantEquals(propertyFolderName));
    

    Which complains about IMedia not having GetPagedChildren and c.Name not being available and the docs aren't helping atm.

    Any advice would be appreciated.

    / Craig

  • Joep 96 posts 698 karma points
    May 25, 2020 @ 19:12
    Joep
    102

    Hi,

    var propertiesRootFolder = mediaService.GetRootMedia().FirstOrDefault(m => m.Name.InvariantEquals("Properties"));
    
    IMedia propertyFolder = mediaService.GetPagedChildren(propertiesRootFolder.Id, 0, int.MaxValue, out var folderCount).Where(x => x.ContentType.Alias == Constants.Conventions.MediaTypes.Folder).FirstOrDefault(c => c.Name.InvariantEquals(propertyFolderName));'
    

    I think the above will solve the problem.

    -Joep

  • Craig100 1136 posts 2523 karma points c-trib
    May 26, 2020 @ 13:21
    Craig100
    0

    Hi Joep,

    Thanks for that. I had to add Umbraco.Core to the Constants.Conventions.MediaTypes.Folder. I don't understand why though because there is a using statement for Umbraco.Core at the top of the code. I seem to trip over quite a few of these issues with V8 so far. Wonder if there's some scope I don't see or understand.

    / Craig

  • Joep 96 posts 698 karma points
    May 26, 2020 @ 13:36
    Joep
    0

    Hi Craig,

    Hmm.. that's odd. I tried it just by adding the Umbraco.Core using at the top. And it worked fine for me. Maybe it was intellisense screwing around? Or was it a compilation error?

    -Joep

  • Craig100 1136 posts 2523 karma points c-trib
    May 26, 2020 @ 13:47
    Craig100
    0

    The code builds ok, but if I take Umbraco.Core off the Constants.Conventions then I get a red squiggly under "Constants" and "Name" further along the line.

    Found the issue..... I had a "using Serilog.Core;" in as well as I'm struggling to get logging working (that's another thread ;) ). If I take it out, then we're all good without the extra inline Umbraco.Core :)

    Thanks :)

Please Sign in or register to post replies

Write your reply to:

Draft