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 ?
/// <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> }
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.
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?)"
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
Try this extension method on IPublishedContent:
Than you can use it like this in razor:
Jeroen
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
Hi Jeroen, thanks!
Can I add this extension method in a normal Umbraco install? Where should I put it?
greetings,
Anthony
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
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?)"
That's because it's an extension method and will only work if you add the Umbraco.Web namespace.
Jeroen
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")
}
}
}
}
Another way to get the data, without an extension method is:
foreach( var item in Model.Content.AsDynamic().mediaArticleAuthors)
{
<li>@item.InnerText</li>
}
is working on a reply...