I wonder if anyone has a quick answer to a question? I'm listing a series of nodes. The below code works perfectly. However, each node is basically a doctype with a download attached. How do I render the link (linkToMedia) to the attached file as a nice url?
LinkToMedia is the name of the property within the document type. This only returns a number which I imagine is the id of the media item. I need it to render the url of the media item attached using the media picker.
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
var blogPosts = Umbraco.Content(1854);
foreach(var blogPost in blogPosts.Children().OrderBy("CreateDate desc").Take(1)){
var mediaItem = Umbraco.TypedMedia(blogPost.linkToMedia);
<div class="item-snippet-footer">
<li>
<h4>@blogPost.itemTitle</h4>
<p>@Umbraco.Truncate(blogPost.articleContent, 100, true)</p>
<a href="@mediaItem.Url">Read More</a>
</li>
</div>
}
}
Basically linkToMedia is the node id of the Media item. Using Umbraco (which is an UmbracoHelper class instance) you can get the media IPublishedContent item which has the property Url, the Url to the media item.
I hope that helps. That's a pretty good gotcha for people using Media Picker in code. :)
This works! Thanks for looking at this for me - I've been working with an existing site and thought all of these nodes had attachments so it's useful to know about the null value.
Here's another thing with this wee snippet. The truncate property is stripping out the text between tags. I've looked through the forum and found a couple of options - namely this sort of thing: @Library.Truncate(@Library.StripHtml(content.GetProperty("bodytext").Value.ToString(), "p"), 250, false) but it's not working - is there an alternative property I can try to bring in a bit of text including html elements?
Launch attached file using @blogPost.NiceUrl()
Hi Folks,
I wonder if anyone has a quick answer to a question? I'm listing a series of nodes. The below code works perfectly. However, each node is basically a doctype with a download attached. How do I render the link (linkToMedia) to the attached file as a nice url?
Thanks in advance. Darren
I'm a wee bit further on this! I've tried this:
LinkToMedia is the name of the property within the document type. This only returns a number which I imagine is the id of the media item. I need it to render the url of the media item attached using the media picker.
Any ideas would be greatly appreciated.
Thank you! Darren
Hey Darren,
You could try the following:
Basically linkToMedia is the node id of the Media item. Using Umbraco (which is an UmbracoHelper class instance) you can get the media IPublishedContent item which has the property Url, the Url to the media item.
I hope that helps. That's a pretty good gotcha for people using Media Picker in code. :)
Thanks,
Jamie
Hi Jamie,
Thanks for the speedy response!
Unfortunately, the code doesn't work. Getting the following error:
Error loading Partial View script (file: ~/Views/MacroPartials/News-circular.cshtml)
The mediaItem variable seem to be fine it's the Read More that's causing the problem. I'm probably doing something wrong!
Thanks again Darren
What's your error? Check ~/App_Data/Logs/UmbracoTraceLog.txt and post out the error.
Could be a null exception when no media id is provided or simply that node got deleted. In any case the mediaItem is probably null :/
probably best to wrap the link in a
Hi Jamie,
This works! Thanks for looking at this for me - I've been working with an existing site and thought all of these nodes had attachments so it's useful to know about the null value.
Thanks again Darren
Nothing is safe from the null ;) Even if you make a property in the CMS mandatory there is a likelihood an author could delete the referenced node.
Glad you got it sorted out. :)
Hi Jamie,
Here's another thing with this wee snippet. The truncate property is stripping out the text between tags. I've looked through the forum and found a couple of options - namely this sort of thing: @Library.Truncate(@Library.StripHtml(content.GetProperty("bodytext").Value.ToString(), "p"), 250, false) but it's not working - is there an alternative property I can try to bring in a bit of text including html elements?
Cheers Darren
is working on a reply...