When using the media picker in Umbraco, it stores the id of the media item.

This post shows you how to get the file path of the item so you can use it for the src of an image or as a link to a document.

Here is how to do it inside a view or partial view:

UmbracoHelper uHelper = new UmbracoHelper(UmbracoContext.Current);
string mediaUrl = "";
if (CurrentPage.HasValue(propertyName))
{
    var mediaItem = uHelper.Media(CurrentPage.propertyName.ToString());
    mediaUrl = mediaItem.umbracoFile;
}
return mediaUrl;

Here is how to do it from a method in a class somewhere:

using Umbraco.Web;

public static string GetMediaUrlFromPropertyName(dynamic contentItem, string propertyName)
{
    UmbracoHelper uHelper = new UmbracoHelper(UmbracoContext.Current);
    string mediaUrl = "";
    if (contentItem.HasValue(propertyName))
    {
        var mediaItem = uHelper.Media(contentItem.GetPropertyValue(propertyName));
        mediaUrl = mediaItem.umbracoFile;
    }
    return mediaUrl;
}

I hope you found this useful. Let me know if you need any further help with this.

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

Paul Seal

A .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Web: codeshare.co.uk

Comments

Share Top