Copied to clipboard

Flag this post as spam?

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


  • Tom Maton 387 posts 660 karma points
    Jan 19, 2016 @ 20:40
    Tom Maton
    0

    Getting media folder by Guid and not Id

    Hi All,

    I'm trying to see if its possible to get the media by Guid rather than Id?

    I'm asking as Courier transfers over media folders and the ids are different, there for breaking the live site and making the courier deployment somewhat useless for some parts of the site.

    Thanks, Tom

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Jan 19, 2016 @ 21:16
    Nicholas Westby
    0

    This should work:

    var service = Umbraco.Core.ApplicationContext.Current.Services.MediaService;
    // Keep in mind this is slow (hits the database).
    var mediaContent = service.GetById(new Guid("00000000000000000000000000000000"));
    var id = mediaContent.Id;
    var helper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    var publishedMedia = helper.TypedMedia(id);
    

    Though, I wouldn't recommend that approach. Also, you might want to test the TypedMedia function to see if it accepts the string representation of a GUID (I know it accepts a string parameter, but that might just be the string version of the integer ID).

    I'd recommend using some configuration to store media ID's. This could be in an app setting in the web.config or it could be a custom section in the web.config. It could also be something you store in a settings content node.

    I tend to use pickers whenever I deal with media, so I'm not exactly sure what your use case is. Depending on your use case, there may be a better way.

  • Tom Maton 387 posts 660 karma points
    Jan 19, 2016 @ 21:25
    Tom Maton
    0

    Hi Nicholas,

    Thanks for this, I also tend to use pickers for media, but my problem is when on the staging server the folder picked is id:1301 but when we use Courier to deploy the media folders the same folder has an id:1221 on live so then none of the content is shown hence why the thinking behind the Guid as this matches across both environments.

    Stupid question but what do you mean by storing in settings content node? Do you mean by storing it in a standard node? If so wouldn't this still be the issue I mentioned above?

    I guess the only way to ensure it still works when deploying is to store it as an app setting :(

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Jan 19, 2016 @ 21:47
    Nicholas Westby
    0

    My assumption was that you were hardcoding media ID's in your source code.

    If you have media picker picks a media node that has an ID on staging of 1234 and that on live has an ID of 4321, but that has the same GUID on both environments, your media picker property should have the appropriate media ID on each environment (i.e., staging would have 1234 and live would have 4321). However, that will only work so long as you also use Courier to transfer your content nodes that have the media pickers on them.

    This works for all the built-in content property types (e.g., media pickers) and is extensible using a concept called Courier resolvers. For example, the Archetype package has a companion DLL you can optionally install that comes with a Courier resolver. That ensures that content (media or content nodes) picked from within Archetype properties gets migrated and changed appropriately per environment.

Please Sign in or register to post replies

Write your reply to:

Draft