Copied to clipboard

Flag this post as spam?

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


  • Barry Fogarty 493 posts 1129 karma points
    Sep 02, 2011 @ 00:29
    Barry Fogarty
    0

    Razor script to iterate through uComponents multi URL picker nodes

    Can anyone lend a hand getting me started with a razor script to loop through a multi URL?  The XML data of my node is as follows:

    <multi-url-picker>
      <url-picker mode="URL">
        <new-window>False</new-window>
        <node-id />
        <url>www.google.com</url>
        <link-title />
      </url-picker>
      <url-picker mode="Content">
        <new-window>False</new-window>
        <node-id>1094</node-id>
        <url>/about-us/</url>
        <link-title />
      </url-picker>
    </multi-url-picker>

    Based on another post from Ismail I got to this point:

    var relatedLinksNode = Model.relatedLinks;

    foreach (XElement item in relatedLinksNode.BaseElement.Elements("url-picker"))
            {
                if (item.Attribute("mode").Value == "url")
                {
                    <li><a href="@umbraco.library.NiceUrl(int.Parse(item.Attribute("url").Value))">[title]</a></li>
                }
                else
                {
                    <li><a href="@item.Attribute("url").Value" rel="external">[title]</a></li>
                }
            }

    but not having any joy with it, I have tried every possible BaseElement value, removing the dashes etc, but I never get any items return from the list.

    Thing is I am puzzled as to how Ismail's code could work, although maybe the uComponenets team have refactored the node names since his post.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 02, 2011 @ 08:55
    Sebastiaan Janssen
    2

    I use this (using 4.7.1: I just copied the umbraco.MacroEngines.dll from latest nightly build of umbraco 4.7.1. http://nightly.umbraco.org/umbraco%204.7/4.7%20RC/), Considering that the property alias for the multi url picker in my case is "urlPicker":

      foreach (var link in Model.UrlPicker.urlpicker)
      {
        <a href="@link.url" @Html.Raw(link.newwindow == "True" ? "target=\"_blank\"" : "")>@link.linktitle</a><br />
      }
  • Barry Fogarty 493 posts 1129 karma points
    Sep 02, 2011 @ 10:40
    Barry Fogarty
    0

    Thanks Sebastiaan - I hereby declare you my Person of the Week!  Lovely clean solution, that one has been bugging me for ages.  To clarify for other readers, based on the data above my code is as follows (where multi url picker alias is 'relatedLinks')

    foreach (var link in Model.relatedLinks.urlpicker)
    {
       
    <a href="@link.url"@Html.Raw(link.newwindow =="True"?"target=\"_blank\"":"")>@link.linktitle</a><br />
    }
  • Mike Taylor 155 posts 353 karma points
    Jan 13, 2012 @ 13:03
    Mike Taylor
    0

    Hi there

    I'm trying to create a generic Razor script where you select the Multi URL picker field in the macro (using a propertyTypePicker).

    So, ideally, I'd like to do something like this:

     

    foreach(var link inModel.getProperty(Parameter.linkField).urlpicker)
    {
       
    <a href="@link.url"@Html.Raw(link.newwindow =="True"?"target=\"_blank\"":"")>@link.linktitle</a><br />
    }

     

    But this doesn't work.

    Any ideas?

    Mike

  • Bruce Canino 21 posts 42 karma points
    Feb 02, 2012 @ 19:53
    Bruce Canino
    0

    I found this while looking for help with getting the values from the Mult URL picker and I got it to work.

    My question is ( it might be a noob razor question) is why if the xml node name is link-title? why do you refrer to it as linktitle in razor.

    When I had link-title, I would be back no node with link

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Feb 04, 2012 @ 13:01
    Sebastiaan Janssen
    0

    Because the name of the xml node needs to be translated to a C# method name and method names cannot have a regular dash in them.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 04, 2012 @ 16:20
    Jeroen Breuer
    0

    This issue was fixed in 4.7.1, but still has a bug in 4.7.1.1: http://umbraco.codeplex.com/workitem/30680. It will be fixed in the next release or you can already copy over the latest umbraco.MacroEngines.dll from the nightlies.

    Jeroen

  • djscorch 67 posts 106 karma points
    Mar 19, 2012 @ 18:03
    djscorch
    0

    Is it possible to check if the urlpicker has any items?

    Model.relatedLinks.urlpicker.Count() causes an error if the user hasn't picked any.

  • Peter Cort Larsen 421 posts 1038 karma points
    Jul 07, 2012 @ 14:38
    Peter Cort Larsen
    0

    Hi,

    I have a node with a property which uses a datatype of type: Multi-Url Picker.

    I can get the values using the code below, but how do i make it recursive?
    I want to show the same links on child nodes.

     if (Model.HasValue("topNavigationLinks"))
    {
    foreach (var link in Model.topNavigationLinks.urlpicker)
    {
    <li><a href="@link.url"@Html.Raw(link.newwindow == "True" ? "target=\"_blank\"" : "")>@link.linktitle</a></li>
    }
    }
  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 10, 2012 @ 16:07
    Simon Dingley
    0

    You could try something like this:

      var topNavLinks = @Model._topNavigationLinks;
    
      if (!string.IsNullOrEmpty(topNavLinks))
      {
          foreach (var link in Model.topNavigationLinks.urlpicker)
          {
              <li><a href="@link.url"@Html.Raw(link.newwindow == "True" ? "target=\"_blank\"" : "")>@link.linktitle</a></li>
          }
      }

    This is untested but should give you an idea. Note the underscore before the property name to make it walk up the tree recursively. I believe this convention exists since v4.7.1.

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Jul 10, 2012 @ 16:10
    Simon Dingley
    0

    Sorry slight mistake in the above but this crappy forum editor won't let me edit it! See the amended version below:

    var topNavLinks =@Model._topNavigationLinks;

     
    if(!string.IsNullOrEmpty(topNavLinks))
     
    {
         
    foreach(var link in topNavLinks.urlpicker)
         
    {
             
    <li><a href="@link.url"@Html.Raw(link.newwindow =="True"?"target=\"_blank\"":"")>@link.linktitle</a>li>
         
    }
     
    }

     

  • Peter Cort Larsen 421 posts 1038 karma points
    Jul 11, 2012 @ 23:28
    Peter Cort Larsen
    0

    Hi,

    I ended up doing it like this:

     

    @inherits umbraco.MacroEngines.

     

    DynamicNodeContext

     

    @

    using umbraco.MacroEngines;

     

    @

    using uComponents.Core.DataTypes.MultiUrlPicker.Dto;

     

    @{

     

    MultiUrlPickerState selectedLinks = GlobalHelpers.GetMultiUrlPickerState(Model.GetProperty("topNavigationLinks", true).Value);

     

     

     

    if (selectedLinks.Items.Any())

     

    {

     

    foreach (var link in selectedLinks.Items)

     

    {

     

    var target = link.NewWindow ? "target=\"_blank\"" : "";

     

     

     

    <li><ahref="@link.Url"@target>@link.Title</a></li>

     

    }

     

    }

     

    //<multi-url-picker><url-picker mode="URL"><new-window>False</new-window><node-id /><url>http://www.domain1.com</url><link-title>Name1</link-title></url-picker><url-picker mode="URL"><new-window>False</new-window><node-id /><url>domain2</url><link-title>name2</link-title></url-picker></multi-url-picker>

     

    }

     

  • AlexRamsey 4 posts 24 karma points
    Dec 07, 2012 @ 05:12
    AlexRamsey
    0

    Hi,

    I also have a node with a property which uses a datatype of type uComponents Multi-Url Picker. I'm able to get the values from the Multi-Url Picker from the current page or model using the code below, however I am unable to make it recursive. Can anyone suggest a way of doing this?

    (I am using Umbraco version 4.11.1)

    @inherits umbraco.MacroEngines.DynamicNodeContext             
    @using umbraco.MacroEngines;             
    @using uComponents.DataTypes.MultiUrlPicker.Dto;
    @{
        if (Model.HasValue("selectPods"))
        {
            var podSelection = Model.GetProperty("selectPods", true).Value;
            MultiUrlPickerState pods = @GlobalHelpers.GetMultiUrlPickerState(podSelection);

            if (pods.Items.Any())
            {
                foreach (var item in pods.Items)
                {
                    var itemId = item.NodeId.ToString();
                    var itemNode = Library.NodeById(itemId);
                    if (!String.IsNullOrEmpty(itemId))
                    {
                        var pageNode = Model.NodeById(itemId);
                        if (pageNode.HasValue("podContent"))
                        {
                            var pageLink = pageNode.NiceUrl;
                            var pageTitle = pageNode.GetProperty("pageTitle").Value;
                            var podTitle = (!String.IsNullOrEmpty(pageTitle)) ? pageTitle : pageNode.Name;
                            var itemIntro = pageNode.GetProperty("podContent").Value;
                            string itemImage = pageNode.GetProperty("podImage").Value;
                           
                            var podImageThumb = Library.ToDynamicXml(itemImage);
                            foreach (var podItem in podImageThumb)
                            {
                                var image = podItem.Image;
                                          
                                <article class="column">
                                    <figure><a href="@pageLink"><img src="@image.umbracoFile" alt="@pageTitle" /></a></figure>
                                    <h2>@podTitle</h2>
               
                                    <p>@(Html.Raw(@itemIntro))</p>
                                    <a href="@pageLink" class="more">Read more ›</a>
                                </article> 
                            }
                        }
                    }
                }
            }
        }
    }



  • Anthony Candaele 1197 posts 2049 karma points
    Jan 20, 2013 @ 09:17
    Anthony Candaele
    0

    Hi,

    When I try to loop through the uComponent Multi-URL Picker links like this:

    foreach (var link in Model.uBlogsyPostLinks.urlpicker) {

    ....

    }

    I get the following error on 'Model.uBlogsyPostLinks':

    'uComponents.DataTypes.MultiUrlPicker.Dto.MultiUrlPickerState' does not contain a definition for 'urlpicker'

    Is there something I'm missing here?

    My Umbraco version is 4.11.3.1 and my version of uComponents is 3.5

    Thanks for your help,

    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 20, 2013 @ 09:58
    Anthony Candaele
    0

    Hi,

    I using Umbraco v4.11.3.1 and uComponents v3.5

    When I try to loop through the related links like this:

    foreach(var link in Model.uBlogsyPostLinks.urlpicker)

    {

    ....

    }

    I get the following runtime error: 'umbraco.MacroEngines.DynamicXml' does not contain a definition for 'urlpicker'

    Has there been something changed, so that the solution by Sebastiaan doesn't work anymore?

    Thanks for your help,

    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    Jan 20, 2013 @ 18:19
    Anthony Candaele
    0

    Anyone has a clue what's going on here:

    the code:

    foreach (var link in Model.UrlPicker.urlpicker)
     ....

    result in the following runtime error:

    'umbraco.MacroEngines.DynamicXml' does not contain a definition for 'urlpicker'

    I'm using Umbraco v.4.11.3.1 and uComponents v3.5

    Thanks for your help,

    Anthony 

  • bob baty-barr 1180 posts 1294 karma points MVP
    Feb 19, 2013 @ 17:11
    bob baty-barr
    0

    Anthony, did you get anywere with this? i am bumping into exact same issue.

  • Ravi Motha 290 posts 500 karma points MVP 8x c-trib
    Feb 19, 2013 @ 17:37
    Ravi Motha
    2

    I've been happily using the code from  the razor macro package by  Sebastiaan Janssen and the code I have working is based on 

     

            @foreach (var link in Model.relatedResources.urlpicker)        {
     //do stuff based on link mode 
     
    @if (link.mode.ToLower() == "url"){

    }

    }
    you should be fine then to use the right data format (XML) and that should do it?? oh and relatedresources is my name for the datatype 
  • bob baty-barr 1180 posts 1294 karma points MVP
    Feb 19, 2013 @ 17:42
    bob baty-barr
    0

    thanks Ravi!

  • John Ligtenberg 53 posts 214 karma points
    Jun 28, 2013 @ 15:31
    John Ligtenberg
    2

    I'm showing a list of Multi-URL Picker links in a partial view in Umbraco 6 MVC, and the Model.relatedResources.urlpicker syntax doesn't work because the model of the partial view is IPublishedContent.

    Currently I'm using this helper to show the links, but I wonder if there is a better solution:

    @*
        Show a list of links based on the value of a
        uComponents Multi-URL Picker.
       
        include using uComponents.DataTypes.MultiUrlPicker.Dto
    *@

    @helper ShowMultiUrlPickerList(IPublishedContent node, string fieldAlias)
    {   
        if (node.HasValue(fieldAlias))
        {       
            string linkList = node.GetProperty(fieldAlias).Value.ToString();
    MultiUrlPickerState items = MultiUrlPickerState.Deserialize(linkList);
           
            if (items.Items.Any())
            {
                <p>
                    <ul>
                        @foreach (UrlPickerState item in items.Items)
                        {
    var linkUrl = item.Url;
                            var linkTarget = item.NewWindow ? " target=\"_blank\"" : String.Empty;
                   
                            <li><a href="@linkUrl"@Html.Raw(linkTarget)>@item.Title</a></li>
                   
                        }
                    </ul>
                </p>          
            }       
        }
    }
  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jul 03, 2013 @ 19:43
    Alex Skrypnyk
    0

    Hi John,

    I tried your method to do that, but MultiUrlPickerState.Deserialize method returns empty collection always ((

    I don't know what to do.

    Any suggestions ?

    THanks,

    Alex

  • John Ligtenberg 53 posts 214 karma points
    Jul 04, 2013 @ 10:35
    John Ligtenberg
    0

    Hi Alex, 

    What does the content of your "linklist" string look like? E.g. mine is:

    <multi-url-picker>
      <url-picker mode="Content">
        <new-window>False</new-window>
        <node-id>1061</node-id>
        <url>/bezoekersinformatie/algemene-informatie.aspx</url>
        <link-title>Openingstijden</link-title>
      </url-picker>
      <url-picker mode="Content">
        <new-window>False</new-window>
        <node-id>1062</node-id>
        <url>/bezoekersinformatie/entreeprijzen.aspx</url>
        <link-title>Entreeprijzen</link-title>
      </url-picker>
      <url-picker mode="URL">
        <new-window>False</new-window>
        <node-id />
        <url>http://www.kit.nl/</url>;
        <link-title>Contact</link-title>
      </url-picker>
    </multi-url-picker>
    
    I'm using UComponents 6.0.0-build1086 with Umbraco 6.1.1
     
  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jul 04, 2013 @ 11:15
    Alex Skrypnyk
    0

    My is : 

    <links><link title=\"Первая страница\" link=\"1083\" type=\"internal\" newwindow=\"0\" /><link title=\"Вторая\" link=\"2412\" type=\"internal\" newwindow=\"0\" /></links>
  • John Ligtenberg 53 posts 214 karma points
    Jul 04, 2013 @ 12:45
    John Ligtenberg
    1

    It looks like you are using a different datatype for your field. Mine is: "uComponents: Multi-URL Picker".

    Could it be that yours is: "Multi-Node Tree Picker" ? (I'm referring to the value of the Property editor field in the configuration of the datatype.)

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Jul 04, 2013 @ 14:11
    Alex Skrypnyk
    0

    My is "Related Links" )) so it's the problem I see.

    Thanks

  • Ben Wyatt 3 posts 23 karma points
    Jul 20, 2013 @ 15:01
    Ben Wyatt
    0

    Hi guys

    For what it's worth, this seems to work for me:

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

    {

    <li><a href="@link.Url">Link</a></li>

    }

    Ben

     

Please Sign in or register to post replies

Write your reply to:

Draft