All my templates inherit from a single master template, where I want to display the header logo, but for some reason it won't display it when i insert the code for retrieving media items.
@{
var typedMediaPickerSingle = Model.Content.GetPropertyValue<IPublishedContent>("headerLogo");
if (typedMediaPickerSingle != null)
{
<a href="index.html"><img src='@typedMediaPickerSingle.Url" style="width:320px" alt="@typedMediaPickerSingle.GetPropertyValue("headerLogo")' /></a>
}
}
Sorry for the horrible formatting, I don't know why the Umbraco Forum editor does this. It looks fine in my project.
Depending on which node within that tree you are on you will need to reference the node that has the 'headerLogo' property.
To test do something like this:
var DKnode = Umbraco.TypedContent(123) //Use the ID of the node that has the header logo
var typedMediaPickerSingle = DKnode.GetPropertyValue<IPublishedContent>("headerLogo");
Depending on the version, that method may or may not work. The above ways of getting the media items rely on property value converters. Also, is this an image upload or media picker? If it's an image upload, I believe that the URL will automatically be returned, but if its a media picker, basically you have to get the ID and run it through Umbraco.TypedMedia(int id) to get back the media item as IPublishedContent ( I think that's right).
int imageId = node.GetPropertyValue<int>("headerImage");
var image = Umbraco.TypedMedia(imageId);
var imageUrl = imageItem.Url;
If you want it recursive, just add true as the second argument in GetPropertyValue
Can't access media picker property in content node
I want to display my header logo on all pages from a master template, but I am having trouble getting the media URL from the document type.
My header logo resides in this content node: http://i.imgur.com/QuugP1J.png
Which has the template set as master.
The document type for the page is located here in the tree: http://i.imgur.com/6XZbecu.png
All my templates inherit from a single master template, where I want to display the header logo, but for some reason it won't display it when i insert the code for retrieving media items.
Sorry for the horrible formatting, I don't know why the Umbraco Forum editor does this. It looks fine in my project.
Depending on which node within that tree you are on you will need to reference the node that has the 'headerLogo' property.
To test do something like this:
That should return the header logo
Nothing wrong with the above method, but I thought I would add a way to do it without putting a hard-coded id into the code.
So alternatively you could try:
Again, should do exactly the same as the above but wont have hard-coded id, just in case your ids change per environment.
Hi Thomas,
Depending on the version, that method may or may not work. The above ways of getting the media items rely on property value converters. Also, is this an image upload or media picker? If it's an image upload, I believe that the URL will automatically be returned, but if its a media picker, basically you have to get the ID and run it through Umbraco.TypedMedia(int id) to get back the media item as IPublishedContent ( I think that's right).
If you want it recursive, just add true as the second argument in GetPropertyValue
Umbraco 7.6 introduced new ways of getting URLs, honestly I haven't used those methods enough to have them memorized, but here's a link that I keep handy: http://www.codeshare.co.uk/blog/how-to-get-the-file-path-of-a-media-item-in-umbraco/
Also make sure you have propertyvalueconverters enabled in your umbracosettings.config.
is working on a reply...