Copied to clipboard

Flag this post as spam?

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


  • Andy Felton 185 posts 484 karma points c-trib
    Jun 07, 2013 @ 13:30
    Andy Felton
    0

    Razor and Related links with Media

    Hello,

    I have been trying to access a property of my current page and am struggling with the XML manipulation. The property is of datatype Related links with Media and am running Umbraco 6.1.0.

    I can get the property and it contains an XML string as follows:

    <links>
     <link title="Results" link="http://www.kbsuk.com/optimist/iocaevents/index.asp?EventID=48" type="external" newwindow="1" />
    </links>

    The type of the string is dynamicXML.

    I can get to the link element by using links.link and get to attributes correctly using links.link.title. The issue is when I do links.link.link it returns me exactly the same as links.link - is there any way around that without me having to removing fields?

    Also is there a way to get the count of how many link elements are within links?

    Any help would be greatly appreciated.

    Regards

    Andy

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 07, 2013 @ 13:35
    Jeavon Leopold
    1

    Hey Andy,

    Did you take a look at the sample code on the the Related Links documentation page here?

    Thanks,

    Jeavon

  • Andy Felton 185 posts 484 karma points c-trib
    Jun 07, 2013 @ 16:26
    Andy Felton
    0

    Hi Jeavon,

    Thanks for pointing that out - I'd looked at the Related Links with media and not the normal Related links - all working now!

    Andy

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 07, 2013 @ 17:14
    Jeavon Leopold
    0

    Hi Andy,

    That's great! If you did want to get a count of the elements, you should able to do that by using .Count on the DynamicXml object. e.g. if you were using the Mvc Dynamic sample: 

    if(CurrentPage.HasValue("relatedLinks") && CurrentPage.relatedLinks.Any()){
    if (CurrentPage.releatedLinks.Count() > 4){ <ul> @foreach (var item in CurrentPage.relatedLinks){ var linkUrl = (item.type.Equals("internal")) ? @Umbraco.NiceUrl(int.Parse(item.link)) : item.link; var linkTarget = (item.newwindow.Equals("1")) ? " target=\"_blank\"" : String.Empty; <li><a href="@linkUrl"@Html.Raw(linkTarget)>@item.title</a></li> } </ul>
    } }

    Additionaly, I would recommend you check out using uComponents Multi-URL Picker, I has a lot better UI that the Core Related Links picker and I think it will be included in Umbraco v6.2 Core. http://ucomponents.org/data-types/multi-url-picker/

    Thanks,

    Jeavon

  • John Halsey 59 posts 220 karma points
    Mar 20, 2014 @ 11:30
    John Halsey
    0

    Hello,

    I know this thread is a few months old but am having a slight problem with the relatedLinks data type.

    I have it set up and can list out all the links no problem. Each link (all internal) has a "mainImage" property, and what I want to do is list out the images of the related items, but I can't figure out how to do that, and I can't see anywhere in the documentation that says how.

    Here is my code:

    @{
        if (Model.HasValue("relatedLinks") && Model.relatedLinks.Any())
        {
            <div class="width1680 popularProducts">
                <div class="width960">
                <h1 class="centerText">RELATED PRODUCTS</h1>
    
                @foreach (var item in Model.relatedLinks)
                {
                    var linkUrl = (item.type.Equals("internal")) ? umbraco.library.NiceUrl(int.Parse(item.link)) : item.link;
                    var linkTarget = (item.newwindow.Equals("1")) ? " target=\"_blank\"" : string.Empty;
                    @*var nodeId = item.Id;
                    var image = Library.NodeById(nodeId);*@
    
                    <div class="floatProducts">
                        <div class="sqrProductsImage">
                            <a href="@linkUrl" @Html.Raw(linkTarget)>
                                <img src="@item.Media("mainImage","umbracoFile")" alt="@item.Media("mainImage","altText")"/>
                            </a>
                        </div>
    
                        <div class="overlay"></div>
                        <div class="ic_caption">
                            <h2 class="centerText">@item.Name</h2>  
                        </div>
    
                    </div>
                }
    
                <div class="clearLeft"></div>
                </div>
            </div>
    
        }    
    
    }
    

    I tried to get the node ID of each link but it caused an error so commented it out. The item.Media() in the src of the img tag also doesn't work. Any ideas?

    I tried using the Multi URL picker from uComponents but had great difficulty counting the number of related links so changed it to this (which works better apart from this).

    Any ideas?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Mar 20, 2014 @ 20:57
    Jeavon Leopold
    0

    Hi John,

    So am I right in saying that each node in the related links is internal and each of those nodes has a property called MainImage and that is what you are after?

    If I'm correct then I think this:

    @{
        if (Model.HasValue("relatedLinks") && Model.relatedLinks.Any())
        {
            <div class="width1680 popularProducts">
                <div class="width960">
                    <h1 class="centerText">RELATED PRODUCTS</h1>
    
                    @foreach (var item in Model.relatedLinks)
                    {
                        var linkUrl = (item.type.Equals("internal")) ? umbraco.library.NiceUrl(int.Parse(item.link)) : item.link;
                        var linkTarget = (item.newwindow.Equals("1")) ? " target=\"_blank\"" : string.Empty;
    
    
                        <div class="floatProducts">
                            <div class="sqrProductsImage">
                                <a href="@linkUrl" @Html.Raw(linkTarget)>
    
                                    @{
                                        var page = Library.NodeById(item.link);
                                        if (page.HasValue("mainImage"))
                                        {
                                            var mediaItemId = page.mainImage;
                                            var mediaItem = Library.MediaById(mediaItemId);
    
                                            <img src="@mediaItem.umbracoFile" alt="@mediaItem.altText" />
                                        }
                                    }
    
                                </a>
                            </div>
    
                            <div class="overlay"></div>
                            <div class="ic_caption">
                                <h2 class="centerText">@item.Name</h2>
                            </div>
    
                        </div>
                    }
    
                    <div class="clearLeft"></div>
                </div>
            </div>
    
        }
    
    }
    

    Jeavon

  • John Halsey 59 posts 220 karma points
    Mar 21, 2014 @ 10:22
    John Halsey
    0

    Ah I can see how your getting the id's now. I wasn't aware about the .link property.

    Thanks for your help.

Please Sign in or register to post replies

Write your reply to:

Draft