Copied to clipboard

Flag this post as spam?

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


  • John Walker 43 posts 66 karma points
    Jan 22, 2011 @ 11:19
    John Walker
    0

    Looping over downloads in a user control

    Hi all,

    Im trying to loop over a list of downloads selected from a multi node picker in a user control can anyone point me in the right direction of going about this.

    I know in xslt to do it I simply use;

    <xsl:for-each select="$currentPage/downloads/MultiNodePicker/nodeId">

     

    Thanks

    John

  • Anders Stentebjerg 58 posts 234 karma points
    Jan 22, 2011 @ 11:52
    Anders Stentebjerg
    0

    Hi John,

    I think you need a little more.

    Try something like this:

    <ul>
       <xsl:for-each select="$currentPage/downloads/MultiNodePicker/nodeId">
    <xsl:variable name="node" select="$currentPage/ancestor-or-self::*[@isDoc and @level = 1]/descendant-or-self::*[@isDoc and @id = current()]" />
                      <li>
                        <a href="{umbraco.library:NiceUrl(.)}">
                          <xsl:value-of select="$node/@nodeName" />
                        </a>
                      </li>
                    </xsl:for-each>
                  </ul>

  • John Walker 43 posts 66 karma points
    Jan 22, 2011 @ 11:54
    John Walker
    0

    Hi Anders,

    Maybe I didnt as that very well. I know how to do it in a xslt file no problem in my current scenraio im attempting to do this in a c# user control.

     

    Thanks

    John

  • Anders Stentebjerg 58 posts 234 karma points
    Jan 22, 2011 @ 11:55
    Anders Stentebjerg
    0

    Okay, then I'm not the guy to ask :)

    //Anders

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Jan 23, 2011 @ 17:57
    Hendy Racher
    0

    Hi John,

    I'm guessing you're using the Multi-Node Tree Picker and saving your data as XML ?

    Using the node factory API, from the current node you could loop though each Node in the Node.Children property to find the 'downloads' Node:

    Node currentNode = Node.GetCurrent();
    Node downloadsNode = null;

    foreach (Node childNode in currentNode.Children)
    {
    if (childNode.NodeTypeAlias == "downloads")
    {
    downloadsNode = childNode;
    break;
    }
    }

    With the downloadsNode containg the MultiNodePicker property, the following will return the XML string stored by the MultiNodePicker:
    eg. "<MultiNodePicker><nodeId>1</nodeId><nodeId>2</nodeId></MultiNodePicker>"

    string pickerValue = string.Empty;
    Property pickerProperty = downloadsNode.GetProperty("MultiNodePicker");

    if (pickerProperty != null)
    {
    pickerValue = pickerProperty.Value;
    }

    To process this XML string and return a collection of Nodes:

    List<Node> pickedNodes = new List<Node>();
    Node node;
    int id;

    using (XmlReader xmlReader = XmlReader.Create(new StringReader(pickerValue)))
    {
    xmlReader.Read();
    if(xmlReader.Name == "MultiNodePicker")
      {
    // Position on first <nodeId>
        xmlReader.ReadStartElement();

    while (!xmlReader.EOF)
        {
        if(xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "nodeId")
          {
        node = new Node(xmlReader.ReadElementContentAsInt())
            if (node != null)
            {
            pickedNodes.Add(node);
            }
          }
    else
    {
    // Step the reader on
            xmlReader.Read();
    }                    
    }
    }
    }

    Here's an example using the uQuery helper library which allows Linq queries to navigate the node structure, handles the null checks and creates a Node collection from a Multi-Node Tree Picker Xml snippet:

    Node downloadsNode = uQuery.GetCurrentNode()
                              .GetChildNodes()
                              .Where(node => node.NodeTypeAlias == "downloads")
                              .FirstOrDefault();

    if(downloadsNode != null)
    {
      List<Node> pickedNodes =
    uQuery.GetNodesByXml(downloadsNode.GetProperty<string>("MultiNodePicker"));
    }


    HTH,

    Hendy

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies