Copied to clipboard

Flag this post as spam?

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


  • jonok 297 posts 657 karma points
    Aug 02, 2011 @ 11:21
    jonok
    0

    Retrieve URL Picker data in Razor

    What is the correct Razor code for retrieving the data stored in the uComponents URL Picker? I want to use a conditional statement to check what type of link and whether it opens in a new window, but I can't find any examples that I have been able to use (or understand).

  • Sander Houttekier 114 posts 163 karma points
    Aug 04, 2011 @ 16:58
    Sander Houttekier
    0

    I would very mutch like to know how to do this ...

    i found how to get the url, but i can't access the new-window or link-title like this, because razor does not recognize these xml elements with the '-' in the middlee.

    so here is what i have so far:

    var cs = Model.LinkToCustomerPage;
    var csUrl cs.url;
    var csNewWindow cs.new-window;

     

    sadly, the first 2 lines work correctly, razor discovers the xml and makes it available trough '.' notation,   so  cs.url

    but as soon as this '-' comes in, it stops working

     

    so i would need tofind a solution for that too

    best regards
    Sander Houttekier

  • Sander Houttekier 114 posts 163 karma points
    Aug 05, 2011 @ 10:24
    Sander Houttekier
    0

    I found a way to do it, but i'm not really sure this is the best way.

    you can use xpath to fetch the correct element inside the xml content.

    so, to correct my code in the above post:

    varcs =Model.LinkToCustomerPage;
    varcsUrl =cs.url;  // this way can be used for url but not for new-window because of the '-' in new-window.
    var NewUrl = @cs.XPath("//new-window"); // returns 'True' or 'False' as a string.

     

     best regards
    Sander Houttekier

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Aug 05, 2011 @ 10:26
    Ismail Mayat
    0

    Sander,

    Here is how i am doing it

    @if (Model.usefulLinks.ToString() != string.Empty)

    {

        <div class="sixcol">

            <ul>

            @foreach (var item in Model.usefulLinks.BaseElement.Elements("link"))

            {

                if (item.Attribute("type").Value == "internal")

                {

                    <li><a href="@umbraco.library.NiceUrl(int.Parse(item.Attribute("link").Value))">@item.Attribute("title").Value</a></li>

                }

                else { 

                    <li><a href="@item.Attribute("link").Value" rel="external">@item.Attribute("title").Value</a></li>

                }                                                                                                                                                                                                                                                                                                                   

            }  

            </ul>

        </div>

    }

    Regards

    Ismail

     

     

  • Mark 4 posts 26 karma points
    Oct 28, 2011 @ 11:50
    Mark
    0

    The Sander Houttekier razor code doesn't work in Umbraco 4.7.1 and doesn't retrieve the correct value but give me the message "umbraco.MacroEngines.DynamicXml

    Anybody know the reason why ? 

  • Sean Dooley 288 posts 527 karma points
    Nov 07, 2011 @ 11:18
    Sean Dooley
    2

    I have set the data format to JSON then used uComponents.Core.DataTypes.UrlPicker.Dto.UrlPickerState.Deserialize method to get the UrlPicker data

    var urlPicker = uComponents.Core.DataTypes.UrlPicker.Dto.UrlPickerState.Deserialize(source.UrlPicker);
    var title = urlPicker.Title
    var mode = urlPicker.Mode // returns 'Content'
    var nodeId = urlPicker.NodeId
    var newWindow = urlPicker.NewWindow // returns 'false'

    Hope this helps

  • Razvan 36 posts 67 karma points
    Nov 27, 2011 @ 16:27
    Razvan
    0

    It seems like the new-window and link-title nodes can be called by newwindow and linktitle

     

     

    @Model.url.BaseElement.Element("linktitle").Value
    @Model.url.BaseElement.Element("newwindow").Value

     

  • Carl Schéle 8 posts 28 karma points
    Nov 29, 2011 @ 22:13
    Carl Schéle
    0

    @Sean Dooley

    How did you even get the access to uComponents.Core.DataTypes.UrlPicker.Dto.UrlPickerState.Deserialize method? What do I need to add into References (i assume)?

  • Sean Dooley 288 posts 527 karma points
    Dec 07, 2011 @ 16:15
    Sean Dooley
    0

    @Carl Schéle

    You shouldn't need to add any references if using the full uComponents.Core.DataTypes.UrlPicker.Dto.UrlPickerState.Deserialize method like mentioned.

  • Sean Dooley 288 posts 527 karma points
    Dec 07, 2011 @ 17:05
    Sean Dooley
    0

    Below is an example of how I've used my sample above using a foreach loop on a MultiUrlPicker

    var items = uComponents.Core.DataTypes.MultiUrlPicker.Dto.MultiUrlPickerState.Deserialize(Model.MultiUrlPicker).Items;
    foreach(var item in items) {
    if(item.Mode == uComponents.Core.DataTypes.UrlPicker.UrlPickerMode.Content) {
    <p>@item.Title - @item.Mode - @item.NodeId - @item.Url - @item.NewWindow</p>
    }
    else if(item.Mode == uComponents.Core.DataTypes.UrlPicker.UrlPickerMode.Media) {
    <p>@item.Title - @item.Mode - @item.NodeId - @item.Url - @item.NewWindow</p>
    }
    else if(item.Mode == uComponents.Core.DataTypes.UrlPicker.UrlPickerMode.Upload) {
    <p>@item.Title - @item.Mode - @item.NodeId - @item.Url - @item.NewWindow</p>
    }
    else if(item.Mode == uComponents.Core.DataTypes.UrlPicker.UrlPickerMode.URL) {
    <p>@item.Title - @item.Mode - @item.NodeId - @item.Url - @item.NewWindow</p>
    }
    }

    Hope this helps

  • Douglas Ludlow 210 posts 366 karma points
    Dec 21, 2011 @ 21:09
    Douglas Ludlow
    5

    It's actually simpler than that. In 4.7.1, I'm able to access the properties with Razor in the following fashion (when the property alias is urlPicker):

    @Model.urlPicker.mode


    @Model.urlPicker.linktitle


    @Model.urlPicker.url


    @Model.urlPicker.nodeid


    @Model.urlPicker.newwindow

    Just make sure that the data format is set to Xml or @Model.urlPicker will return a string instead of an umbraco.MacroEngines.DynamicXml object.

  • Funka! 398 posts 661 karma points
    Apr 12, 2012 @ 04:44
    Funka!
    0

    Douglas Ludlow, thank you!  Your four-month-old post is still pertinent, works great, and saved me much hassle! So easy, just not so discoverable!

  • michael Netonline 18 posts 62 karma points
    Jun 12, 2012 @ 09:05
    michael Netonline
    0

    I want to use the url picker for a media Image this is my code. My document property is always null or gives me a string like this: Truehttp://www.google.nl/ what is wrong? This code works well in the content nodes.

     

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @using uComponents.Core

    @using uComponents.Core.uQueryExtensions

     

    @{

      var mediaFolder = Model.MediaById(Parameter.Map);

      

     

        @foreach (var n in mediaFolder.Children)

        {

            var media = n.umbracoFile.ToString();

            var href = "";

            var blank = "";

            var urlPicker = n.url; //The Document property

           

            if (urlPicker != null){ 

              

              blank = (urlPicker.newwindow.ToString() == "True") ? "target=\"_blank\"" : "" ;

          

              string mode = urlPicker.mode.ToString();                                                                                                            

              switch(mode){

                case "Content":    

                     href = Model.NodeById(urlPicker.nodeid).Url.ToString();

                     break;

                case "Media":

                case "Upload":

                     href = Model.MediaById(urlPicker.nodeid).umbracoFile.ToString();

                    break;

                case "URL":

                    href = urlPicker.url.ToString();

                    break;

                default:

                    href = "";

                    break;

              }

            }        

                                     

        }

     

     }

  • michael Netonline 18 posts 62 karma points
    Jun 12, 2012 @ 09:44
    michael Netonline
    0

    I have figured it out :)

     

     @inherits umbraco.MacroEngines.DynamicNodeContext

    @using uComponents.Core

    @using uComponents.Core.uQueryExtensions

     

    @{

        umbraco.cms.businesslogic.media.Media mediaFolder = uQuery.GetMedia(Convert.ToInt32(Parameter.Map));

      

      <div class="rotate">

        @foreach (var n in mediaFolder.Children)

        {

            var media = n.getProperty("umbracoFile").Value.ToString();

            var href = "";

            var blank = "";

            var urlPicker = uComponents.Core.DataTypes.UrlPicker.Dto.UrlPickerState.Deserialize(n.getProperty("url").Value.ToString()); //The Document property

           

            if (urlPicker != null){ 

              

              blank = urlPicker.NewWindow ? "target=\"_blank\"" : "" ;

     

              string mode = urlPicker.Mode.ToString();                                                                                                            

              switch(mode){

                case "Content":    

                     href = Model.NodeById(urlPicker.NodeId).Url.ToString();

                     break;

                case "Media":

                case "Upload":

                     href = Model.MediaById(urlPicker.NodeId).umbracoFile.ToString();

                    break;

                case "URL":

                    href = urlPicker.Url.ToString();

                    break;

                default:

                    href = "";

                    break;

              }

            }        

            

     

            <a style="display: block;position: absolute; left: 0; top: 0;" href="@href" @Html.Raw(blank)><img src="@media" alt="" /></a>                          

        }

      </div>

     }

     

  • Chris Gaskell 59 posts 142 karma points
    Aug 10, 2012 @ 16:06
    Chris Gaskell
    1

    For reference here's the codee I use to load a URL from the URLPicker.

    I pass the alias of the URLPicker field and a Css Class into the macro.

     

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @using uComponents.Core.DataTypes.UrlPicker.Dto;       

    @{

      var propertyName = Parameter.URLPickerFieldAlias;

      var link = UrlPickerState.Deserialize(Model.GetProperty(propertyName).Value);

      string linkHTML;

      linkHTML = string.Format("{3}",

                  link.Url,

                  link.NewWindow ? " target=\"_blank\"" : "",

                  string.IsNullOrWhiteSpace(Parameter.Css_Class) ? "" : " class=\"" + Parameter.Css_Class + "\"",

                  link.Title

              );

    }

    @Html.Raw(linkHTML)

     

  • Funka! 398 posts 661 karma points
    Sep 26, 2012 @ 00:35
    Funka!
    0

    After upgrading a site from 4.7.2 to 4.9.0 (and uninstalling uComponents v3 and installing uComponents v5 beta), we found our razor script crashing, with the following message:

    'uComponents.DataTypes.UrlPicker.Dto.UrlPickerState' does not contain a definition for 'url'

    The fix was surprising:  we capitalized "url" to "Url" and it started working again. (That is, we changed @node.myUrlPicker.url to @node.myUrlPicker.Url)

  • Anthony Southworth 46 posts 173 karma points
    Oct 01, 2012 @ 13:17
    Anthony Southworth
    1

    After upgrading to Umbraco 4.9 and uComponents 5 I was struggling to get the Multi-Url Picker values, but if you have Razor Model Binding set then the following works:

    @foreach(var link in Model._footerLinks1.Items)

    {

    <li><a href="@link.Url" @Html.Raw(link.NewWindow ? "target=\"_blank\"" : "")>@link.Title</a></li>

    }

  • Pete 213 posts 285 karma points
    Nov 28, 2012 @ 17:31
    Pete
    0

    I'm using this, but I keep getting the domain infront on the chosen url?

    Any ideas?

    its showing www.mydomain.com/www.google.com.

    <a href="@item.linkUrl.Url">Link</a>
  • Douglas Ludlow 210 posts 366 karma points
    Nov 28, 2012 @ 19:13
    Douglas Ludlow
    0

    @Pete, you'll need to include the "http://" in the url that you add in with the picker, otherwise it will think it's a relative url and try to navigate to it as if it is in your site.

  • Craig100 1136 posts 2522 karma points c-trib
    May 21, 2013 @ 18:21
    Craig100
    0

    Any ideas why this doesn't work or how I can get it to work? It's off a 4.7.1 site. The urlPicker isn't recognised. It's a multi-url picker of alias "magazineLink".

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
      foreach(var page in Model.Children.Take(1){
         
        var image page.Media("magazineCover","umbracoFile");
        var urlPicker page.magazineLink.urlpicker;   
                                                  
        <div class="magazine-cover">
           <href="@urlPicker.url" title="" target="">
           <img src="/ImageGen.ashx?image=@image&constrain=true&pad=false" /></a>
        </div
        <div class="magazine-description">
             <h2>@page.magazineTitle</h2>
             @page.magazineDescription      
        </div>                             
      }
                                                   
    }

    Cheers,

    Craig

  • Funka! 398 posts 661 karma points
    May 24, 2013 @ 02:52
    Funka!
    0

    Hi Craig,

    Are you getting an error message, or simply no output at all? Or are you getting output, but missing a piece of it?

    (To see error messages and C# razor stack traces, you need to (1) enable debugging and tracing in Web.config, and (2) add ?umbDebugShowTrace=true to the end of your page URL.)

    Without knowing your document type structure and property names, etc., I'm not seeing anything that jumps out at me. Although,... maybe .url should be .Url? These are case-sensitive I believe.

    In any case, best of luck to you and I hope you get this resolved.

  • Craig100 1136 posts 2522 karma points c-trib
    May 24, 2013 @ 10:52
    Craig100
    0

    I was getting no output at all.  However, due to time pressure I replaced the datatype with a text box. Not ideal, but had to get the site modification done.

    Cheers,

    Craig

  • Eric Schrepel 161 posts 226 karma points
    Sep 07, 2013 @ 01:21
    Eric Schrepel
    3

    Capitalization of properties is key; the following Razor works in Umbraco 6.1.x with the URLPicker uComponent:

    @* (No longer need to include any "using uComponents" statements at the top) *@

    @Model.urlPicker.Mode
    @Model.urlPicker.Title@* not linktitle as previously reported *@
    @Model.urlPicker.Url
    @Model.urlPicker.NodeId
    @Model.urlPicker.NewWindow
  • Anthony Candaele 1197 posts 2049 karma points
    Sep 13, 2013 @ 11:41
    Anthony Candaele
    0

    @Eric

    I tried this in my Partial View Macro File:

     

    if (Model.Content.GetProperty("mediaArticleLink").ToString() != String.Empty)

                    {                    

                        <a href="@Model.urlPicker.Url" title="@Model.urlPicker.Title" target="_blank">

                           @Model.urlPicker.Title

                        </a>

                    }

                    else{                    

                        <a href="@Model.urlPicker.Url" target="_blank">@Model.urlPicker.Url</a>

                    }

     

    But the compiler doesn't recognize the urlPicker property:

    "Error222'Umbraco.Web.Models.PartialViewMacroModel' does not contain a definition for 'urlPicker' and no extension method 'urlPicker' accepting a first argument of type 'Umbraco.Web.Models.PartialViewMacroModel' could be found (are you missing a using directive or an assembly reference?)"

     

    I'm working with an Umbraco 6.1.5 install an uComponents v5.5.0

    any help is much appreciated,

    Anthony

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Sep 13, 2013 @ 16:48
    Ali Sheikh Taheri
    1

    solution is ( for the all the properties with dash) for umbraco 6 use CurrentPage and for v4 Model

    @CurrentPage.urlPicker.url
    @CurrentPage.urlPicker.XPath("link-title").InnerText
    @CurrentPage.urlPicker.XPath("new-window").InnerText
    @CurrentPage.urlPicker.XPath("node-id").InnerText
    
  • Eric Schrepel 161 posts 226 karma points
    Sep 14, 2013 @ 20:20
    Eric Schrepel
    0

    Anthony, sorry I wasn't very clear with my code. "urlPicker" happens to be the name of our field/property that we use to store the URL Picker datatype.

    It looks like your URL Picker field is named mediaArticleLink, so just replace "urlPicker" with "mediaArticleLink" in your above code.

    I'm a little unclear what the if/then statement is doing, but regardless, just substituting the fieldname should help. Feel free to reply back if that doesn't solve it.

    And Ali's right, you can swap "CurrentPage" for "Model" throughout any Umbraco 6+ code (I just forget to use that newer syntax).

  • Streety 358 posts 568 karma points
    Mar 03, 2014 @ 12:08
Please Sign in or register to post replies

Write your reply to:

Draft