Copied to clipboard

Flag this post as spam?

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


  • Anthony Candaele 1197 posts 2049 karma points
    Sep 12, 2013 @ 11:28
    Anthony Candaele
    0

    how to loop through uComponents multiple textstring objects

    Hi,

    For a MediaArticle documenttype, I'm using a property (authors) of type uComponents Multiple Textstrng

    in my Partial View Macro File I want to loop through all the authors of an article like this:

    foreach (var article in articles)

                {

                    if (article.HasValue("mediaArticleAuthors"))

                    {

                        <address>

                          @foreach (var author in article.mediaArticleAuthors)

                           {

     

                           }

                        </address>

                    }

                    <p>

     

                        @article.GetPropertyValue("pageTitle")

     

                    </p>

                }

            }

     

    this used to work, but not anymore in v6 Razor, the property mediaArticleAuthors isn't recognized on the the article object which is of type IPublished content.

    Any knows how to loop through uComponents Multi Textstring properties in v6 Razor ?

    Thanks for your help,

    Anthony

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 12, 2013 @ 12:48
    Jeroen Breuer
    100

    Try this extension method on IPublishedContent:

    /// <summary>
    /// Return the strings that has been filled in.
    /// </summary>
    /// <param name="content"></param>
    /// <param name="alias"></param>
    /// <returns></returns>
    public static IEnumerable<string> GetMultipleTextStrings(this IPublishedContent content, string alias)
    {
        var xml = content.GetPropertyValue<RawXElement>(alias);
    
        if (xml != null)
        {
            var xmlData = xml.Value;
    
            if (xmlData != null)
            {
                return
                (
                    from x in xmlData.Descendants("value")
                    select x.Value
                );
            }
        }
    
        return Enumerable.Empty<string>();
    }

    Than you can use it like this in razor:

    @foreach(var s in Model.Content.GetMultipleTextStrings("alias"))
    {
    <p>@s</p>

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 12, 2013 @ 12:54
    Jeroen Breuer
    1

    The reason your current code doesn't work is because article.mediaArticleAuthors is dyanmic and you can't use that if you use IPublishedContent strongly typed.

    Jeroen

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 12, 2013 @ 14:30
    Anthony Candaele
    0

    Hi Jeroen, thanks!

    Can I add this extension method in a normal Umbraco install? Where should I put it?

    greetings,

    Anthony

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 12, 2013 @ 14:33
    Jeroen Breuer
    0

    I added the extension method in a separate project and added the dll to the bin folder, but you could also place it in the App_Code folder.

    Jeroen

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 12, 2013 @ 14:49
    Anthony Candaele
    0

    I add your GetMultipleTextstrings() method in a CustomExtensions class in the App_Code folder:

        using System;

        using System.Collections.Generic;

        using System.Linq;

        using System.Web;

        using Umbraco.Core.Dynamics;

     

        public class CustomExtensions

        {

            /// <summary>

            /// Return the strings that has been filled in.

            /// </summary>

            /// <param name="content"></param>

            /// <param name="alias"></param>

            /// <returns></returns>

            public static IEnumerable<string> GetMultipleTextStrings(this Umbraco.Core.Models.IPublishedContent content, string alias)

            {

                var xml = content.GetPropertyValue<RawXElement>(alias);

     

                if (xml != null)

                {

                    var xmlData = xml.Value;

     

                    if (xmlData != null)

                    {

                        return

                        (

                            from x in xmlData.Descendants("value")

                            select x.Value

                        );

                    }

                }

     

                return Enumerable.Empty<string>();

            }

        } 

     

     

    However, Vistual Studio is complaining about this statement:

    content.GetPropertyValue

    the error message is:

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

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 12, 2013 @ 14:53
    Jeroen Breuer
    0

    That's because it's an extension method and will only work if you add the Umbraco.Web namespace.

    Jeroen

  • Anthony Candaele 1197 posts 2049 karma points
    Sep 12, 2013 @ 15:22
    Anthony Candaele
    0

    It's working, very cool!!:

     

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @{

        var yearfolders = Model.Content.Children.Where("DocumentTypeAlias == \"MediaArticleYearFolder\"");

        foreach (var yearfolder in yearfolders)

        {

           

    @yearfolder.Name

            var articles = yearfolder.Children.Where("DocumentTypeAlias == \"MediaArticle\"");

            if (articles.Count() > 0)

            {

                foreach (var article in articles)

                {

                   

     

                    @if (article.HasValue("mediaArticleAuthors"))

                    {                    

                         foreach (var author in article.GetMultipleTextStrings("mediaArticleAuthors"))

                           {

                               @author @:,

                           }                     

                    }                   

                    @article.GetPropertyValue("pageTitle")

                   

                }

            }

        }   

    }

  • organic 108 posts 157 karma points
    Oct 23, 2013 @ 21:55
    organic
    0

    Another way to get the data, without an extension method is:

     

                                foreach( var item in Model.Content.AsDynamic().mediaArticleAuthors)
                                {
                                    <li>@item.InnerText</li>
                                }

Please Sign in or register to post replies

Write your reply to:

Draft