Copied to clipboard

Flag this post as spam?

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


  • Ehsan 8 posts 30 karma points
    Nov 30, 2014 @ 07:10
    Ehsan
    0

    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.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Nov 30, 2014 @ 10:25
    Jan Skovgaard
    0

    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

  • jivan thapa 194 posts 681 karma points
    Nov 30, 2014 @ 10:36
    jivan thapa
    2

    You need to convert data into json format.

    Here is one example,

    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);
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Dec 01, 2014 @ 00:04
    Jeavon Leopold
    0

    @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.

  • Ehsan 8 posts 30 karma points
    Dec 07, 2014 @ 20:33
    Ehsan
    0

    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

     

     

  • Ehsan 8 posts 30 karma points
    Dec 13, 2014 @ 06:30
    Ehsan
    0

    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:

    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.

    cheeers.

  • jivan thapa 194 posts 681 karma points
    Dec 13, 2014 @ 10:58
    jivan thapa
    0

    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 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.

    :)

  • Ehsan 8 posts 30 karma points
    Dec 13, 2014 @ 18:23
    Ehsan
    1

    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.

  • jivan thapa 194 posts 681 karma points
    Dec 14, 2014 @ 09:39
    jivan thapa
    0

    @Ehsan

    I am happy that your problem has been fixed.

    Happy coding.

    :)

Please Sign in or register to post replies

Write your reply to:

Draft