Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Darren Wilson 234 posts 622 karma points
    Oct 20, 2015 @ 11:52
    Darren Wilson
    0

    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?

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
        @{ var blogPosts = Umbraco.Content(1854); foreach(var blogPost in blogPosts.Children().OrderBy("CreateDate desc").Take(1)){ <div class="item-snippet-footer">
      <li>
                    <h4>@blogPost.itemTitle</h4>
                    <p>@Umbraco.Truncate(blogPost.articleContent, 100, true)</p>
                    <a href="@blogPost.NiceUrl()">Read More</a>
    
      </li>
                         </div>}}
    

    Thanks in advance. Darren

  • Darren Wilson 234 posts 622 karma points
    Oct 21, 2015 @ 15:55
    Darren Wilson
    0

    I'm a wee bit further on this! I've tried this:

    <a href="@blogPost.linkToMedia">Read More</a>
    

    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

  • Jamie Pollock 174 posts 853 karma points c-trib
    Oct 21, 2015 @ 16:06
    Jamie Pollock
    0

    Hey Darren,
    You could try the following:

    @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. :)

    Thanks,
    Jamie

  • Darren Wilson 234 posts 622 karma points
    Oct 21, 2015 @ 16:18
    Darren Wilson
    0

    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

  • Jamie Pollock 174 posts 853 karma points c-trib
    Oct 21, 2015 @ 16:38
    Jamie Pollock
    100

    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

    @if(mediaItem != null) {
        <a href="@mediaItem.Url">Read More</a>
    }
    
  • Darren Wilson 234 posts 622 karma points
    Oct 22, 2015 @ 09:02
    Darren Wilson
    0

    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

  • Jamie Pollock 174 posts 853 karma points c-trib
    Oct 22, 2015 @ 10:49
    Jamie Pollock
    0

    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. :)

  • Darren Wilson 234 posts 622 karma points
    Nov 02, 2015 @ 15:08
    Darren Wilson
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft