Copied to clipboard

Flag this post as spam?

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


  • Mike Poole 37 posts 139 karma points
    Mar 20, 2018 @ 15:28
    Mike Poole
    0

    Get URL from Link Picker property type

    I have a Link Picker property on my document type which allows the user to enter an external URL value or pick a content item from the Umbraco site (property name PrivacyContentLink

    Within Razor, how do I get the URL regardless of which has been entered?

    I have tried

    if (Model.SiteRoot.GetProperty("PrivacyContentLink").HasValue)
        {
            var privacyNodeId = Model.Content.GetPropertyValue<int>("PrivacyContentLink");
            privacyLink = umbraco.library.NiceUrl(privacyNodeId);
        }
    

    And...

    if (Model.SiteRoot.GetProperty("PrivacyContentLink ").HasValue)
    {
        tcLink = Umbraco.TypedContent(Model.Content.GetPropertyValue<int>(" PrivacyContentLink ")).Url;
    }
    

    All with no luck

    What is the best Razor approach to obtain the value to add to my anchor href attribute?

  • Alex Skrypnyk 4887 posts 17640 karma points MVP 2x admin c-trib
    Mar 20, 2018 @ 15:34
    Alex Skrypnyk
    0

    Hi Mike

    What type of property editor are you using?

    Is it Link Picker - https://our.umbraco.org/projects/backoffice-extensions/link-picker/?

    /Alex

  • Sowndar M 6 posts 76 karma points
    1 week ago
    Sowndar M
    0

    enter image description here enter image description here

    For @link Output show

    Blockquote

    [ { "name": "View Website", "url": "www.google.com", "target": "_blank", "icon": "icon-link" } ]

    How to get the Url ?

  • Alex Skrypnyk 4887 posts 17640 karma points MVP 2x admin c-trib
    Mar 20, 2018 @ 15:35
    Alex Skrypnyk
    0

    If it that one, use this code, one of this codes has to work:

    // Strongly typed
    LinkPicker link = Model.Content.GetPropertyValue("link");
    
    // ModelsBuilder
    var site = Model.Content.Site().OfType();
    var link = site.Link;
    
  • Mike Poole 37 posts 139 karma points
    Mar 20, 2018 @ 15:40
    Mike Poole
    0

    Yes, it is the Gibe.LinkPicker property type. Thanks Alex

    I don't understand your final reply - how do I get the URL of the selected value?

    So if I insert www.google.com into the URL or select an existing Umbraco node?

  • Alex Skrypnyk 4887 posts 17640 karma points MVP 2x admin c-trib
    Mar 20, 2018 @ 16:04
  • Alex Skrypnyk 4887 posts 17640 karma points MVP 2x admin c-trib
    Mar 20, 2018 @ 16:14
    Alex Skrypnyk
    0

    Link Picker is not compatible with latest Umbraco.

  • Mike Poole 37 posts 139 karma points
    Mar 20, 2018 @ 16:31
    Mike Poole
    0

    Which one is not compatibleenter image description here? I am running 7.5.9 at present

    When I debug, I can get a handle on the property and see there is a value

    How do I retrieve the url value from the JPropertyList?

  • Alex Skrypnyk 4887 posts 17640 karma points MVP 2x admin c-trib
    Mar 20, 2018 @ 16:52
    Alex Skrypnyk
    1

    This one should work:

    var linkPicker = JsonConvert.DeserializeObject<LinkPickerModel>(Model.Content.GetPropertyValue<string>("PrivacyContentLink"));
    
            if (linkPicker != null)
            {
                <a href="@linkPicker.Url" target="@linkPicker.Target" class="link">@linkPicker.Name<span></span></a>
            }
    

    Also add this class:

    public class LinkPickerModel
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Url { get; set; }
            public string Target { get; set; }
            public string Hashtarget { get; set; }
        }
    
  • Bjarne Fyrstenborg 1099 posts 2930 karma points MVP 2x c-trib
    Mar 20, 2018 @ 19:24
    Bjarne Fyrstenborg
    1

    Hi Mike

    I think the Link Picker package should still work in latest Umbraco versions - we are using it in several projects.

    Previously you had to include you own class (either inside the razor view or in a separate class file) to reflect the json data and then serialize the json data to this model.

    However if you are using latest version of the package it should have this class included https://github.com/Gibe/Umbraco-Link-Picker/blob/master/src/Gibe.LinkPicker.Umbraco/Models/LinkPicker.cs and also the property value converter https://github.com/Gibe/Umbraco-Link-Picker/blob/master/src/Gibe.LinkPicker.Umbraco/PropertyConverters/LinkPickerValueConverter.cs

    So you should be able to use this:

    @using Gibe.LinkPicker.Umbraco.Models;
    
    LinkPicker link = Model.Content.GetPropertyValue<LinkPicker>("link");
    

    or with ModelsBuilder enabled:

    var link = Model.Content.Link
    

    where "link" is your property alias.

    However we are mostly using another package Multi Url Picker: https://our.umbraco.org/projects/backoffice-extensions/multi-url-picker/ since it can be configurated with minimum and maximum links (so it can act both as Single and Multi Url Picker) and it also seems to work better on Umbraco Cloud.

    Furthermore there is an open pull request for this on Umbraco repository https://github.com/umbraco/Umbraco-CMS/pull/2323 .. so it may be included in core in an upcoming release.

    /Bjarne

Please Sign in or register to post replies

Write your reply to:

Draft