Getting strongly typed Related Links in Umbraco V6
Hello guys,
I can't for the life of me get Related Links out of IPublishedContent in V6.0.6 (I know this is very old, I'm converting dynamics to strongly typed). Can anyone help me please? I've been googling for days.
I know the property type is Umbraco.Web.Models.XmlPublishedContentProperty and I've got the string but I don't know what to parse it with.
Sorry, this code is a mess, I know it won't work with JArray:
var thePage = Model.Content;
var thePageID = thePage.Id;
IPublishedContent theStronglyTypedPage = Umbraco.TypedContent(thePageID);
var typedRelatedLinksConverted = Umbraco.TypedContent(Model.Content.GetPropertyValue<string>("relatedLinks").Split(','));
var typedLinksList = typedRelatedLinksConverted.ToList();
//var typedLinksArray = Umbraco.TypedContent(Model.Content.GetPropertyValue<JArray>("relatedLinks"));
//var typedLinksString = Model.Content.Properties.Where(x => x.GetPropertyValue<string>("relatedLinks") == true);
if (theStronglyTypedPage.HasProperty("relatedLinks") && theStronglyTypedPage.HasValue("relatedLinks"))
{
var i = 0;
var x = theStronglyTypedPage.GetPropertyValue<string>("relatedLinks");
foreach (var thing in Model.Content.GetPropertyValue<JArray>("relatedLinks"))
{
var linkUrl = (thing.GetPropertyValue<bool>("isInternal")) ? Umbraco.NiceUrl(thing.GetPropertyValue<int>("internal")) : thing.GetPropertyValue<string>("link");
var linkTarget = thing.GetPropertyValue<bool>("newWindow") ? "_blank" : null;
var linkType = (thing.GetPropertyValue<bool>("internal")) ? "book" : "external-link";
<ul class="list-group" style="font-size:small">
<li class="list-group-item active"><h3>Related links track external test</h3></li>
<li><a href="@linkUrl" target="@linkTarget">@(thing.GetPropertyValue<string>("caption"))</a></li>
<li class="list-group-item">
@if (linkType == "external-link")
{
<a href="@linkUrl" @Html.Raw(linkTarget)>@(thing.GetPropertyValue<string>("title")) <abbr class="ext" title="External website">ext</abbr></a>
}
else
{
<a href="@linkUrl" @Html.Raw(linkTarget)>@(thing.GetPropertyValue<string>("title")) </a>
}
</li>
}
</ul>}
<ul class="list-group" style="font-size:small">
<li class="list-group-item active"><h3>Related links track external test</h3></li>
for i = 0 to x.length
<li>@x.ToString();</li>
</ul>
}
}
I keep going round in circles trying different things, getting different errors:
`Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'title' and no extension method 'title' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)`
foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'
Yes I think I do, but I am not quite sure how to do that. I have put it in the App_Code folder but it 'expects class, delegate, enum, interface or struct'.
Do I need to make an IEnumerable
Would you know of any links to tutorials to help me understand what to do with it please? My skills need improvement!
Getting strongly typed Related Links in Umbraco V6
Hello guys,
I can't for the life of me get Related Links out of IPublishedContent in V6.0.6 (I know this is very old, I'm converting dynamics to strongly typed). Can anyone help me please? I've been googling for days.
I know the property type is Umbraco.Web.Models.XmlPublishedContentProperty and I've got the string but I don't know what to parse it with.
Sorry, this code is a mess, I know it won't work with JArray:
@using Umbraco.Web @using Umbraco.Web.Models @using Newtonsoft.Json.Linq @{
} }
I keep going round in circles trying different things, getting different errors:
foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'
Hi kleptbit
Just installed Umbraco V6.0.6 ))
Hi,
I have done this before, but I created my own custom logic for handling this.
You will need a DTO and some logic to handle parsing the data returned from the RelatedLinks property.
I will see if I can find my old code for you...
Craig
Oh you guys, thank you! I had little hope of a response, because it's so old.
The site is big, the client won't upgrade and performance is suffering badly, hence me trying to strongly type all the things...
My version:
Found it...
Could do with being refactored, couple years old now:
Hope this helps OR gives you an idea of the direction to take it.
Good luck!
Craig
Thanks Craig, thanks Alex. I have to go out now but cheers for this, I will find my way later I have no doubt!
And please share with us, what is the solution to this issue )
Have a nice evening!
And you Alex!
Well so far I don't think I can use the JArray. I'm getting an Argument NullException:
so the string isn't getting converted to a JArray and thus the source is null...I think. But hey, it's a new error message, so progress of a sort!
Then you need to use code like Craig suggested, parse value manually.
Yes I think I do, but I am not quite sure how to do that. I have put it in the App_Code folder but it 'expects class, delegate, enum, interface or struct'.
Do I need to make an IEnumerable
Would you know of any links to tutorials to help me understand what to do with it please? My skills need improvement!
Sorry for the basic questions.
is working on a reply...