Copied to clipboard

Flag this post as spam?

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


  • Mubeen 15 posts 126 karma points
    Dec 08, 2020 @ 10:41
    Mubeen
    0

    SetValue of a content node's property of type Neasted Content.

    Hi,

    I am trying to set the property of a content node. This property is of type Nested Content.

        nestedStr = nestedStr != null ? nestedStr.Replace("]", ",{ \"name\":\"Item " + (nestedResult.Count + 1) + "\",\"ncContentTypeAlias\":\"" + link.ncContentTypeAlias + "\",\"relationType\":\"" + type + "\",\"linkedElement\":\"[{\"name\":\"" + node.Name + "\",\"udi\":\"" + udi + "\"}]\"}]")
                                           : "[{ \"name\":\"Item " + (nestedResult.Count + 1) + "\",\"ncContentTypeAlias\":\"" + link.ncContentTypeAlias + "\",\"relationType\":\"" + type + "\",\"linkedElement\":\"[{\"name\":\"" + node.Name + "\",\"udi\":\"" + udi + "\"}]\"}]";
        linkedContent.SetValue("linkedElements", nestedStr);
        sender.SaveAndPublishWithStatus(linkedContent);
    

    But i am getting the following error when i am trying to publish the content node.

    Newtonsoft.Json.JsonReaderException: 'After parsing a value an unexpected character was encountered: n. Path '[0].linkedElement', line 1, position 98.'

    Can anyone please help me find out the issue?

  • Mubeen 15 posts 126 karma points
    Dec 09, 2020 @ 06:07
    Mubeen
    100

    Okay so solution was to make the Objects in C# and Serialize them using "JsonConvert.SerializeObject()". Rather than constructing a string which is prone to error. So the new code looks something like this

                       //create new element to be added
                        var lst = new List<Element>();
                        lst.Add(new Element() 
                        { 
                        name = node.Name,
                        udi = udi
                        });
                        //create new elemnt wrapper with propoer relationship to be added
                        var obj = new RootObject()
                        {
                            name = "Item " + (nestedResult.Count + 1),
                            ncContentTypeAlias = link.ncContentTypeAlias,
                            relationType = type,
                            linkedElement = JsonConvert.SerializeObject(lst)
                        };
                        nestedResult.Add(obj);
                        nestedStr = JsonConvert.SerializeObject(nestedResult);
                        if (isNew) 
                        {
                            linkedContent.SetValue("linkedElements", nestedStr);
                            sender.SaveAndPublishWithStatus(linkedContent);
                        }
    
Please Sign in or register to post replies

Write your reply to:

Draft