Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I used to create a static utilities class in my projects for getting various things but particularly important, was media and media properties.
After reading the Umbraco Pitfalls I stopped doing that because the utility class would need a static instance of the UmbracoHelper
_umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
However, getting a media url or a media cropUrl from a picker property is a pain. To do it properly you must first check to see if the picker property has a value then you need to check to see if the media id found on the property still points to a valid piece of media (media could have been deleted but the reference stays).
So to get a crop url generally would mean something like this ...
var media = Umbraco.TypedMedia(page.GetPropertyValue<string>(UmbracoPropertyAlias.MainImage));
if (media != null)
listItem.ThumbnailUrl = media.GetCropUrl(CropAlias.ThumbnailImage);
listItem.ThumbnailUrl = string.Empty;
Which is rather long-winded. I would much rather have the methods from my old utility classes which were something like...
public static string GetMediaCropUrl(int mediaId, string cropAlias)
public static string GetMediaUrl(int mediaId)
public static IPublishedContent GetMedia(string propertyAlias, IPublishedContent node)
Anyway, just interested to know if anybody has found a good way to do this.... extensions maybe?
I've not done this, however, it does sound like it would suite creating extension methods for the UmbracoHelper. I don't imagine you'd have too much of an issue changing your existing methods to become extension methods :-)
What you are looking for here:
I created my media utility class using some methods of this extension class of Hybrid Framework.
Yep, just add extension methods to the Umbraco helper. So, for instance, your GetMediaUrl method would become (something like):
public static string GetMediaUrl(this UmbracoHelper helper, int mediaId)
var media = helper.TypedMedia(mediaId);
return media != null ? media.Url : null;
Remember to create your extensions in a static class.
You'd then just call it like:
string url = Umbraco.GetMediaUrl(1234);
Thanks to all !
All sorted now.
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted