Copied to clipboard

Flag this post as spam?

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


  • Tom 161 posts 322 karma points
    Sep 14, 2018 @ 14:18
    Tom
    0

    Query UmbracoContext.Current.MediaCache by Name

    Hello:

    I am on Umbraco versoin 7.5.9. I see from intellisense in Visual Studio 2017 that UmbracoContext.Current.MediaCache has these properties GetAtRoot, GetById, GetByxPath, GetHashCode, GetSingleByXPath

    I do not see one for "Name".

    Does anyone know how to query the UmbracoContext.Current.MediaCache by name?

    Thanks

    BTD

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Sep 14, 2018 @ 16:23
    Marc Goodson
    1

    Hi Tom

    If you look at the implementation of the MediaCache here:

    https://github.com/umbraco/Umbraco-CMS/blob/1bb593d264a085f94a3ce3bd710392b350561430/src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs

    You'll see that methods like GetByXPath and GetSingleByXPath aren't implemented for Media,

    (this is because Media content isn't stored in an XML file format in the same way that content is (otherwise you could have used either of those methods to create an XPath search for an element with nodeName attribute equal to your search.

    The Media information is stored in the database, but there is an Examine Search layer over the top for speed.

    There is a legacy approach called uQuery: https://github.com/umbraco/Umbraco-CMS/blob/1bb593d264a085f94a3ce3bd710392b350561430/src/Umbraco.Web/umbraco.presentation/umbraco/uQuery/uQuery-Media.cs

    which has a GetMediaByXPath and GetMediaByName, but these will make a request to the db

    what might be quicker is an Examine query of the media section?

    So it depends a little on what you are trying to achieve? eg do you need to find a media item by name? could it be 'picked' or is it Search Media functionality...

    sorry to be a bit vague!

  • Tom 161 posts 322 karma points
    Sep 17, 2018 @ 10:44
    Tom
    0

    Marc: Since all Umbraco Media is stored on the file system, here is what I did. I uploaded the Media (PDF document) to the production media folder. When I had the user do this, it created a unique "id". Next, I combined some Javascript, Angular and a REST service Controller to retrieve the data and return it back as a JSON object using the MediaCache.GetById().

    Its straight forward once you know this id. So for each env (tst, stg, prd), I change the id value programmatically and I'm good to go.

    Thanks for pitching it.

    BTD

Please Sign in or register to post replies

Write your reply to:

Draft