Skip to content

Instantly share code, notes, and snippets.

@warrenbuckley
Created January 30, 2012 16:55
Show Gist options
  • Save warrenbuckley/1705409 to your computer and use it in GitHub Desktop.
Save warrenbuckley/1705409 to your computer and use it in GitHub Desktop.
Umbraco V5 Example - Media Picker
@inherits PartialViewMacroPage
@using Umbraco.Cms.Web
@using Umbraco.Cms.Web.Macros
@using Umbraco.Framework
@{
//Output an image from the media picker
//Property alias of mediaPicker
var mediaPickerPropAlias = String.IsNullOrEmpty(Model.MacroParameters.mediaPickerPropAlias) ? "myMediaPickerPropertyAlias" : Model.MacroParameters.mediaPickerPropAlias;
//Check that the property has a hiveID stored
if (!String.IsNullOrEmpty(DynamicModel[mediaPickerPropAlias]))
{
//Get the image URL
//The HiveID of the selected media item from the media picker
//Then the string property alias of the upload data type on the media type
var imageURL = Umbraco.GetMediaUrl(DynamicModel[mediaPickerPropAlias], "uploadedFile");
//Get the name of the media item
var mediaName = Umbraco.GetDynamicContentById(DynamicModel[mediaPickerPropAlias]).Name;
<img src="@imageURL" alt="@mediaName" />
}
else
{
<p>No media HiveID present</p>
}
}
@warrenbuckley
Copy link
Author

@pepeortiz yes that is also correct but your method is using an Image Upload field with the alias yourImagePropertyName on the current document type.

<img src='@Umbraco.GetMediaUrl(DynamicModel.picture, "uploadedFile")' />

The way above is using a media tree picker property on the current document type which has the alias of picture which stores the ID to the media item stored in the media section. If an image node from the media section was selected then it has a property on that media type which is an upload field which has the property alias of uploadedField

I hope this makes sense the two different approaches.
One is for an upload field on the current document type and the other is using a media picker on the document type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment