bloody RelatedLinks!!! how do you set value to this property - umbraco 7.1.8
I'm fairly new to Umbraco (7.1.8) and I'm building a site on this CMS for the first time. I believe I have picked it up quite well.. everything is going smooth, only thing I can't figure out yet is how to programatically set value to RelatedLinks property feild??
I've looked in to the api and tried many different ways and no luck ! I have searched about it on google and in forums about how to set value to a relatedlinks property and still no answer!
I'm not sure I can answer your question but I just want to encourage you to share some of the code you're currently trying to get working. I think that will increase the chances that some of the C# wizards in here can have a look and tell you what you need to do where in your code for instance.
Create a class that matches to the RelatedLinks's property
public class RLinks
{
public string caption { get; set; }
public string link { get; set; }
public bool newWindow { get; set; }
public bool edit { get; set; }
public bool isInternal { get; set; }
public string type { get; set; }
public string title { get; set; }
}
// get a IContent
var con = ApplicationContext.Current.Services.ContentService.GetById(2103);
// create the RLinks list
var relatedLinks = new List<RLinks>();
// some dummy data
for (var i = 0; i < 4; i++)
{
relatedLinks.Add(new RLinks()
{
caption = "caption" + i,
edit = false,
link = "http://link.link?i=" + i,
isInternal = false,
newWindow = true,
title = "title " + i,
type = "external"
});
}
// encode the object to Json format
var json = new JavaScriptSerializer().Serialize(relatedLinks);
// set the value
con.SetValue("relatedLinks", json);
// it's done
ApplicationContext.Current.Services.ContentService.Save(con);
@jivan example looks like a pretty good start to me!
This question has inspired me to add setters to my existing RelatedLinks Model properties in my Value Converters package so that if you're using the package and you need to do just this you don't have to create yet another model.
Hi Jeavan and Jivan, thank you both for your replies.
@Jeavan, awesome converter package mate! I installed it and started using it. love it!!!
just with the RelatedLinks setters, how do I add new RelatedLinks ?
I'd assume to instantiate a Our.Umbraco.PropertyConverters.Models.RelatedLinks object first but it asks for string propertyData?
and since it returns a IEnumerable then maybe I can add values to it like this instantiatedRelatedLinks.ToList().Add(new Our.Umbraco.PropertyConverters.Models.RelatedLink{ Caption="test", .... })?
@JivanThapa, your solution worked flawlessly with saving a Non-Internal RelatedLinks. THANK YOU. However, I could not figure out how to link up the INTERNAL link. here is my code:
var json = Logic.Helpers.RelatedLinksHelper.RelatedLinksJsonConvertor(new Logic.Helpers.RLinks { caption = item.Quantity + " Qty", title = item.Description, [email protected](), isInternal = true, newWindow = true, edit = false, type = "internal" });
I'm quite sure it's the "link" value I'm setting wrong for internal. looking at umbraco source code, it looks like it's looking for an int Id value.
var obj = JsonConvert.DeserializeObject<JArray>(sourceString);
//update the internal links if we have a context
if (UmbracoContext.Current != null)
{
var helper = new UmbracoHelper(UmbracoContext.Current);
foreach (var a in obj)
{
var type = a.Value<string>("type");
if (type.IsNullOrWhiteSpace() == false)
{
if (type == "internal")
{
var linkId = a.Value<int>("link");
var link = helper.NiceUrl(linkId);
a["link"] = link;
}
}
}
}
@JeavonLeopold, Thank you again for the convertor package. It'd be super awesome if you could add easy to use setters to the RelatedLinks convertor.
Link, should not be a url. It should be id of the internal node. And further RLinks class needs to modify in order to work for internal links. I had not tested before. Now it should work.
I have added two more properties on RLink class. Internal and internalName properties are required for the internal link.
Just notice: you cannot give a property name public string internal, so you have to write public string Internal. I with uppercase letter.
RLinks should look like
public class RLinks
{
public string caption { get; set; }
public string link { get; set; }
public bool newWindow { get; set; }
public bool edit { get; set; }
public bool isInternal { get; set; }
public string type { get; set; }
public string title { get; set; }
public string Internal { get; set; }
public string internalName { get; set; }
}
And here you can set values
// get a IContent
var con = ApplicationContext.Current.Services.ContentService.GetById(2103);
var relatedLinks = new List<RLinks>();
// some dummy data
for (var i = 0; i < 2; i++)
{
relatedLinks.Add(new RLinks()
{
caption = "caption" + i,
edit = false,
link = "2101", // should be id
isInternal = true,
Internal = "2101", // should be id same as link
newWindow = false,
title = "title " + i,
type = "internal",
internalName = "caption" + i
});
}
// encode the object to Json format, Remember to convert Internal to => internal, and isinternal => to isInternal
var json = new JavaScriptSerializer().Serialize(relatedLinks).Replace("Internal", "internal").Replace("isinternal", "isInternal");
con.SetValue("relatedLinks", json);
ApplicationContext.Current.Services.ContentService.Save(con);
Have a look at on the json variable: How I converted "Internal" to 'internal', and 'isinternal' (produce by the first string replacement) => "isInternal". This replacement is necessary to generate valid json format for the internal link. Otherwise it does not work.
In my mind, Jivon Thapa, you just reached the level SUPER AMAZING. It worked! dude you're like ridiculously smart, I hope you running the show where ever you are. You are another inspiration to Umbraco society. I am just speechless. after so many days of trying to figure it out myself. I can't thank you enough buddy! YOU ROCK!!!
Much love from all the way here in Sydney, Australia!
bloody RelatedLinks!!! how do you set value to this property - umbraco 7.1.8
I'm fairly new to Umbraco (7.1.8) and I'm building a site on this CMS for the first time. I believe I have picked it up quite well.. everything is going smooth, only thing I can't figure out yet is how to programatically set value to RelatedLinks property feild??
I've looked in to the api and tried many different ways and no luck ! I have searched about it on google and in forums about how to set value to a relatedlinks property and still no answer!
anyone's help would be extremely appreciated!
thanks for reading.
Hi Ehsan
I'm not sure I can answer your question but I just want to encourage you to share some of the code you're currently trying to get working. I think that will increase the chances that some of the C# wizards in here can have a look and tell you what you need to do where in your code for instance.
Looking forward to hearing from you.
/Jan
You need to convert data into json format.
Here is one example,
Create a class that matches to the RelatedLinks's property
@jivan example looks like a pretty good start to me!
This question has inspired me to add setters to my existing RelatedLinks Model properties in my Value Converters package so that if you're using the package and you need to do just this you don't have to create yet another model.
Hi Jeavan and Jivan, thank you both for your replies.
@Jeavan, awesome converter package mate! I installed it and started using it. love it!!!
just with the RelatedLinks setters, how do I add new RelatedLinks ?
I'd assume to instantiate a Our.Umbraco.PropertyConverters.Models.RelatedLinks object first but it asks for string propertyData?
and since it returns a IEnumerable then maybe I can add values to it like this instantiatedRelatedLinks.ToList().Add(new Our.Umbraco.PropertyConverters.Models.RelatedLink{ Caption="test", .... })?
please shine some light on this. cheers!
Ehsan
Ok just update on where I'm at with this.
@JivanThapa, your solution worked flawlessly with saving a Non-Internal RelatedLinks. THANK YOU. However, I could not figure out how to link up the INTERNAL link. here is my code:
I'm quite sure it's the "link" value I'm setting wrong for internal. looking at umbraco source code, it looks like it's looking for an int Id value.
@JeavonLeopold, Thank you again for the convertor package. It'd be super awesome if you could add easy to use setters to the RelatedLinks convertor.
cheeers.
Hi Ehsan,
Link, should not be a url. It should be id of the internal node. And further RLinks class needs to modify in order to work for internal links. I had not tested before. Now it should work.
I have added two more properties on RLink class.
Internal
andinternalName
properties are required for the internal link. Just notice: you cannot give a property namepublic string internal
, so you have to writepublic string Internal
.I
with uppercase letter.RLinks should look like
And here you can set values
Have a look at on the json variable: How I converted "Internal" to 'internal', and 'isinternal' (produce by the first string replacement) => "isInternal". This replacement is necessary to generate valid json format for the internal link. Otherwise it does not work.
:)
In my mind, Jivon Thapa, you just reached the level SUPER AMAZING. It worked! dude you're like ridiculously smart, I hope you running the show where ever you are. You are another inspiration to Umbraco society. I am just speechless. after so many days of trying to figure it out myself. I can't thank you enough buddy! YOU ROCK!!!
Much love from all the way here in Sydney, Australia!
Long Live Open Source.
Peace out.
Ehsan.
@Ehsan
I am happy that your problem has been fixed.
Happy coding.
:)
is working on a reply...