Copied to clipboard

Flag this post as spam?

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


  • Johan 95 posts 264 karma points
    Dec 07, 2015 @ 15:23
    Johan
    0

    I want to list links and document items on a list. For this purpose, I'm using Related Links and Multiple Media picker.

    I want it to list the items that the user creates and if the user isnt creating the items, then i shouldn't display anything

    This is what I've done so far:

    @inherits UmbracoTemplatePage
    @{
        var navItems = CurrentPage;
    }
    @if (navItems.Any())
    {
                <div class="RightColumn">
                    <div id="LeftMenuHolder">
    
                        <span class="Head-Round-Red">
    
                            <span>Relaterad information</span>
                        </span>
    
    
                        <ul>
                            @foreach (var item in navItems)
                            {
                                <li>
                                    <a href="@item.relateradlaenk">
                                        <span>@item.Name</span>
                                    </a>
                                </li>
                            }
                        </ul>
    
    
                        <span class="BoxBot">
    
                            <span></span>
                        </span>
                    </div>
                </div>
    }
    

    The problem is that it doesn't show anything, even tho I've created several items on the back-office.

    Help please

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 07, 2015 @ 15:27
    Dennis Aaen
    0

    Hi Johan,

    You can find the documentation for the related links data type, and how to get data out from it using dynamic or strongly typed Razor here: https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/related-links

    And the multiple media picker documentation is available here https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/Media-Picker

    Hope this helps,

    /Dennis

  • Johan 95 posts 264 karma points
    Dec 07, 2015 @ 16:03
    Johan
    0

    I tried the following:

     @{
                           if (CurrentPage.HasValue("relateradlaenk") && CurrentPage.relatedLinks.ToString().Length > 2)
        {
            <ul>
                @foreach (var item in CurrentPage.relatedLinks)
                {
                    var linkUrl = (bool)item.isInternal ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.link;
                    var linkTarget = (bool)item.newWindow ? "_blank" : null;
                    <li><a href="@linkUrl" target="@linkTarget">@item.caption</a></li>
                }
            </ul>
        }
    }  
    

    note that "relateradlaenk" is the alias of the Related Link

    I tried it, but but it still doesnt show any values even tho I've created them in the backoffice

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 07, 2015 @ 16:42
    Dennis Aaen
    0

    Hi Johan,

    Try this code below.

     @{
       if (CurrentPage.HasValue("relateradlaenk") && CurrentPage.relateradlaenk.ToString().Length > 2)
        {
            <ul>
                @foreach (var item in CurrentPage.relateradlaenk)
                {
                    var linkUrl = (bool)item.isInternal ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.link;
                    var linkTarget = (bool)item.newWindow ? "_blank" : null;
                    <li><a href="@linkUrl" target="@linkTarget">@item.caption</a></li>
                }
            </ul>
        }
    }  
    

    Hope this works,

    /Dennis

  • Johan 95 posts 264 karma points
    Dec 08, 2015 @ 10:13
    Johan
    0

    Thank you Dennis, it's working!

    Is it possible to link to documents (for example .pdf documents) the same way?

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 08, 2015 @ 10:48
    Dennis Aaen
    0

    Hi Johan,

    If you need this to be possible then you will need to make the pdf documents as a part of your content structure.

    It´s not possible to choose files from the media library with the related links data type.

    /Dennis

  • Johan 95 posts 264 karma points
    Dec 08, 2015 @ 12:49
    Johan
    0

    how do I make the pdf documents as a part of your content structure?

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 08, 2015 @ 13:24
    Dennis Aaen
    0

    Hi Johan,

    You can use the related links if the files that you want to link to is external files. I am not sure that make the pdf documents a part of your content structure is the best solution.

    But what you could do is create a pdf document type, on this document type use a media picker or a upload data type.

    But if you just want to list files from a folder in the media library I would recommend you to use the media picker, on the page, and the select a folder from the media library, and loop through the folder and output the files.

    Hope this helps,

    /Dennis

  • Johan 95 posts 264 karma points
    Dec 08, 2015 @ 15:20
    Johan
    0

    Hi Dennis,

    I tried this but it didn't work:

      @if (CurrentPage.HasValue("relateradokument"))
                                    {
                                <ul>
                                        var caseStudyImagesList = CurrentPage.CaseStudyImages.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                                        var caseStudyImagesCollection = Umbraco.Media(caseStudyImagesList);
    
                                        foreach (var caseStudyImage in caseStudyImagesCollection)
                                        {
                                            //I want it to list the dokucment as links
                                            <li><a href="@caseStudyImagesList" target="@caseStudyImagesCollection">@item.caption</a></li>
                                        }
                                    }
                                </ul>
                        } 
    

    relateradokument is the alias of the media picker.

    I want it to list the dokuments as links, but not sure how to do it.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 08, 2015 @ 16:36
    Dennis Aaen
    0

    Hi Johan,

    This snippet of code below should do it for you. Remember to change @item.Name if you want the link text to be another property.

    @if (CurrentPage.HasValue("relateradokument"))
    {
        @* Get all the media item associated with the id passed in *@
        var media = Umbraco.Media(CurrentPage.relateradokument);
        var selection = media.Children("File");
    
        if (selection.Any())
        {
            <ul>
                @foreach (var item in selection)
                {
                    <li>
                        <a href="@item.Url">@item.Name</a>
                    </li>
                }
            </ul>
        }
    }
    

    Hope this helps,

    /Dennis

  • Johan 95 posts 264 karma points
    Dec 09, 2015 @ 10:58
    Johan
    0

    Hi Dennis,

    This code isnt working for some reason..

    @* Get all the media item associated with the id passed in *@
                                        var media = Umbraco.Media(CurrentPage.relateradokument);
                                        var selection = media.Children("File");
    
                                            <ul>
                                                @foreach (var item in selection)
                                                {
                                                    <li>
                                                        <a href="@item.Url">tusvr</a>
                                                    </li>
                                                }
                                            </ul>
    
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 09, 2015 @ 11:43
    Dennis Aaen
    0

    Hi Johan,

    Did your page have a folder selected, in the media picker.

    You could try break the code down piece for piece to see where it fails. And please check that the property alias of the property is "relateradokument"

    /Dennis

  • Johan 95 posts 264 karma points
    Dec 09, 2015 @ 12:12
    Johan
    0

    Hi Dennis,

    The alias "relateradokument" is corrent,

    The if statement works:

    @if (CurrentPage.HasValue("relateradokument"))
    {
    }
    

    My page have a pdf file selected, not a folder.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 09, 2015 @ 12:22
    Dennis Aaen
    0

    Hi Johan,

    Ah okay that´s where the problem is. With my code you will need to select a folder in the media library, and then the code will list all the items in this folder of the filetype file.

    Please let me know if you only want to pick the a file and not a folder, and loop trough the files in the folder.

    /Dennis

  • Johan 95 posts 264 karma points
    Dec 09, 2015 @ 12:32
    Johan
    0

    Hi Dennis,

    Oh I see, I'm sorry that I wasn't clear from the start.

    I only want to pick the file and not a folder.

    How would the code look like?

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 09, 2015 @ 12:45
    Dennis Aaen
    0

    Hi Johan,

    Okay I am sure we can make that working too.

    Try this code:

    @{      
        if(CurrentPage.HasValue("relateradokument")){                                         
            var mediaItem = Umbraco.Media(CurrentPage.relateradokument);
            <a href="@mediaItem.Url">@mediaItem.Name</a>
        }
    }
    

    Hope this helps,

    /Dennis

  • Johan 95 posts 264 karma points
    Dec 09, 2015 @ 13:03
    Johan
    0

    Thank you Dennis. It's working now if selected one file.

    What if the user wants to pick multiple files (not inside a folder) through Multiple media picker?

    How would the code look like then?

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 09, 2015 @ 13:10
    Dennis Aaen
    100

    Hi Johan,

    Then the code would look like this

    @if (CurrentPage.HasValue("relateradokument"))
    {
        var filesList = CurrentPage.relateradokument.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
        var filesListCollection = Umbraco.Media(filesList);
    
        foreach (var mediaItem in filesListCollection)
        {
             <a href="@mediaItem.Url">@mediaItem.Name</a>
        }
    }
    

    Hope this works for you.

    /Dennis

  • Johan 95 posts 264 karma points
    Dec 09, 2015 @ 13:30
    Johan
    0

    Thank you so much Dennis!

Please Sign in or register to post replies

Write your reply to:

Draft