Copied to clipboard

Flag this post as spam?

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


  • Gordon Saxby 1461 posts 1883 karma points
    Oct 11, 2011 @ 18:01
    Gordon Saxby
    0

    Find Media "folder" by name

    In a User Control - how can I find a "folder" in the media library by its name?

     

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Oct 11, 2011 @ 18:07
    Hendy Racher
    1

    Hi Gordon,

    If you have uComponents installed, how about:

    Media folder = uQuery.GetMediaByName("folder name").FirstOrDefault();

    or

    Media folder = uQuery.GetMediaByType("Folder")
    .Where(x => x.Text == "folder name")
    .FirstOrDefault();

    HTH,

    Hendy

  • Gordon Saxby 1461 posts 1883 karma points
    Oct 11, 2011 @ 18:28
    Gordon Saxby
    0

    Umm, I haven't needed to install uComponents yet ... and I'd rather not if I can avoid it.

    I wanted to link a content document with a media folder by name, programmatically, rather than a content editor having to set it up. However, maybe I would be much better off (make my life easier!) if I put a Media Picker on the content node!

     

  • Ben McKean 272 posts 549 karma points
    Apr 18, 2012 @ 11:52
    Ben McKean
    0

    @Hendy

    Just stumbled across this thread.

    I'm currently using uComponents in my solution. When using uQuery.GetMediaByName I'm using lambda expression to check that a folders exists and if it doesn't create it.

    I'm trying to debug this in VS but I'm getting an error message "Expression cannot contain lambda expressions". Forgive the stupid question but is this VS Immediate Window being rubbish or aren't lambda exprssions possible using uQuery?

    Here is my code:

    List dateFolders = uQuery.GetMediaByName(today.ToString("yyyy-MM-dd")).Where(x => x.ParentId == ParentFolderID).ToList();

    I'm basically checked if a dated folder exists in a specific folder. The reason I perforum this check is there may be a dated folder within another folder, thats why I need to check for ParentID.

    Any help appreciated

    Ben

     

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Apr 18, 2012 @ 12:16
    Hendy Racher
    1

    Hi Ben,

    Yes, uQuery returns collecitons so should work with Linq and lambdas - your code snippet looks valid to me (assuming today, and ParentFolderID exist):

    List<Media> dateFolders = uQuery.GetMediaByName(today.ToString("yyyy-MM-dd")).Where(x => x.ParentId == ParentFolderID).ToList();

    Just tried a lambda in the immediate window and get the same resposne - "Expression cannot contain lambda expressions", so it seems that it's just an issue executing within this context.

    HTH,

    Hendy

  • Ben McKean 272 posts 549 karma points
    Apr 19, 2012 @ 17:53
    Ben McKean
    0

    Ok, cool. Makes sense.

    I've got another query....have you ever experience the GetMediaByName functionality intermittently not return anything?

    Here is the full code block

    List<Media> dateFolders = uQuery.GetMediaByName(today.ToString("yyyy-MM-dd")).Where(x => x.ParentId == ParentFolderID).ToList();
    Media dateFolder = null;
    umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Custom, 0, "datefolder count: " + dateFolders.Count + ". Parent Folder = " + ParentFolderID + ". String to search for = " + today.ToString("yyyy-MM-dd"));
    if (dateFolders.Count == 0) // create new folder
    {
        dateFolder = Media.MakeNew(today.ToString("yyyy-MM-dd"), MediaType.GetByAlias("Folder"), User.GetUser(0), ParentFolderID);
        dateFolder.XmlGenerate(new XmlDocument());
    }
    else
    {
        dateFolder = dateFolders[0];
    }

    This was definitely working yesterday! but it seems now that uQuery.GetMediaByName is only searching in the root of the Media section, it can't see anything in nested folders??

    The umbraco.log.add is for my debugging purposes.

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft