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.
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.
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 :(
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.
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
This should work:
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.
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 :(
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.
is working on a reply...