Copied to clipboard

Flag this post as spam?

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


  • NiQ 24 posts 45 karma points
    Oct 24, 2011 @ 13:29
    NiQ
    0

    Embedded Content in codebehind, get all properties?

    Im using currentNode.GetProperty().Value to get the value from my Embedded Content but how do I get all the properties?

    I have "image", "text" and "link" set but I don't have a clue how to loop through all the images and print out the properties.

    Help please..

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Oct 24, 2011 @ 13:40
    Stefan Kip
    0
    /// <summary>
    /// Resolves the value of an Embedded Content datatype to a .NET collection
    /// </summary>
    /// <param name="embeddedContentXml">The value of the Embedded Content in XML format</param>
    /// <returns>A collection of items with properties</returns>
    public static IEnumerable<IEnumerable<KeyValuePair<stringstring>>> ResolveEmbeddedContent(string embeddedContentXml)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(embeddedContentXml);
    
        foreach (XmlNode node in xmlDoc.SelectNodes("//data/item"))
        {
            List<KeyValuePair<stringstring>> properties = new List<KeyValuePair<stringstring>>();
            foreach (XmlNode property in node.SelectNodes("*"))
            {
                properties.Add(new KeyValuePair<stringstring>(property.Name, property.InnerText));
            }
            yield return properties;
        }
    }
  • Stefan Kip 1614 posts 4131 karma points c-trib
    Oct 24, 2011 @ 13:43
    Stefan Kip
    0

    Btw, here's an exampje of how to use this:

    rptMoreContent.DataSource = DatatypesHelper.ResolveEmbeddedContent(CurrentNode.GetPropertyValue("content")).Select(x => new
    {
        IconUri = MediaHelper.GetMediaUrl(int.Parse(x.First(y => y.Key == "icon").Value)),
        Title = x.First(y => y.Key == "title").Value,
        Description = x.First(y => y.Key == "description").Value.Replace("\n""<br />")
    });
    rptMoreContent.DataBind();
  • Rao 10 posts 30 karma points
    Feb 10, 2012 @ 19:01
    Rao
    0

    Hello,

    I was using the embedded content control in Umbraco. I wanted to read the data in .NET. I have seen your code but I was wondering how can we get the embedded content XML into .NET. Can you please post the full code example.

     

    Thanks,

    Subba.

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Feb 10, 2012 @ 19:17
    Stefan Kip
    0

    Well just use:

    Node.GetCurrent().getProperty("yourPropertyAlias").Value
  • Rao 10 posts 30 karma points
    Feb 10, 2012 @ 20:47
    Rao
    0

    I was using this productNode.GetProperty("embeddedVariant").Value; to get the value and I am getting the embedded content data as: SWhite1000SRed1001MGreen1002

    (which is nothing but 1. Size: S, Color: White, Value: 1000 

    2. Size: S, Color: Red, Value: 1001

    3. Size: M, Color: Green, Value: 1002)

    When I retrieve the values I wanted to get the values as key value pairs as you mentioned in the example.

    Here is the embedded content screenshot:

     

  • MK 429 posts 906 karma points
    Mar 11, 2012 @ 09:48
    MK
    0

    Hi Rao,

    Im using the "productNode.GetProperty("embeddedVariant").Value" and I get the following:

    <data><item id="1"><TestName propertyid="1">Available colours</TestName><TestValue propertyid="2">yellow, green, purple, orange</TestValue><TestLinkedMediaItem propertyid="3">1093</TestLinkedMediaItem><TestLinkedContentItem propertyid="4">1070</TestLinkedContentItem><TestValidFrom propertyid="5">2012-03-13 15:46</TestValidFrom></item><item id="3"><Name propertyid="1">package measurments</Name><Value propertyid="2">50x80x40</Value><LinkedMediaItem propertyid="3">1097</LinkedMediaItem><LinkedContentItem propertyid="4">1068</LinkedContentItem><ValidFrom propertyid="5">2012-03-31 06:13</ValidFrom></item><item id="2"><Name propertyid="1">package weight</Name><Value propertyid="2">50k</Value><LinkedMediaItem propertyid="3">1092</LinkedMediaItem><LinkedContentItem propertyid="4">1064</LinkedContentItem><ValidFrom propertyid="5">2020-03-11 17:40</ValidFrom></item></data>

    Any suggestions on why do I get the tags as well?

    Many thanks

    Mkariti

  • MK 429 posts 906 karma points
    Mar 11, 2012 @ 14:31
    MK
    0

    Ok So I think I've got it - If someone knows better way how to extract the node's property would love to see it.

    Here is my code:

     

     

     
    @using @using System.Xml;
     
    @{
        var currentNode = umbraco.presentation.nodeFactory.Node.GetCurrent();
       var strValue =  @currentNode.GetProperty("productSpecification").Value;
    
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(strValue);
        XmlNode element = xmlDoc.SelectSingleNode("/data");
        
        <ul>
            @foreach(XmlNode item in @element.ChildNodes)
            {
    
       
    
                 <li>
                  @Html.Raw(item["Name"]!=null ? item["Name"].InnerText : ""): 
                  @Html.Raw(item["Value"] != null ? item["Value"].InnerText : "")
                </li>
                         
               
              }
        </ul>
                         
    
            }Regards, 
    mkariti
  • BJ Patel 84 posts 210 karma points
    Jun 28, 2015 @ 17:54
    BJ Patel
    0

    For Displaying String out of HTML.

    @Html.Raw(@productNode.GetPropertyValue("embeddedVariant"))
    
Please Sign in or register to post replies

Write your reply to:

Draft