Copied to clipboard

Flag this post as spam?

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


  • Davit 7 posts 88 karma points
    Apr 22, 2020 @ 12:24
    Davit
    0

    How can I add an item to nested-content (IEnumerable<IPublishedElement>>("usps")) programmatically?

    I have usps nested-content property on-page and I want to add a new item to usps list from backend

    The item property name is usp

    Thanks in advance.

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Apr 22, 2020 @ 12:42
    Alex Skrypnyk
    101

    Hi Davit,

    Something like:

    var list = new List<Dictionary<string, string>>();
    
    Guid guid1 = Guid.NewGuid();
    
    liste.Add(new Dictionary<string, string>(){
        {"key","@guid1"},
        {"name","Some name"},
        {"ncContentTypeAlias","usp"},
        {"Property in NestedContent","Some Text"},
    });
    
    node.SetValue("usps", JsonConvert.SerializeObject(list));
    

    Thanks,

    Alex

  • Rihab 104 posts 388 karma points
    Aug 27, 2020 @ 11:29
    Rihab
    0

    Hi Alex

    I did it your way and it works fine when adding and in the backoffice the values are added, but when I retrieve the value to show it on my website I got this error enter image description here can you help me to solve this error?

  • Kiran 17 posts 109 karma points
    Feb 02, 2021 @ 12:50
    Kiran
    1

    Hi Alex, I am new to Umbraco. How do I get node in your example. Currently i am using umbraco 8.8 . Can you please suggest me. .

  • Davit 7 posts 88 karma points
    Apr 22, 2020 @ 13:14
    Davit
    1

    Alex thank you very much it works perfectly.

    Thanks David

  • BJ Patel 84 posts 210 karma points
    Aug 03, 2021 @ 07:44
    BJ Patel
    0

    I am having almost the same code as that @Alex has advice.

    var cs = Services.ContentService;
    var Parent = cs.GetById(Convert.ToInt32(NodeId));
    
    var uLikes = new List<Dictionary<string, string>>();
    
    uLikes.Add(new Dictionary<string, string>() {
                {"ncContentTypeAlias","userPost"},
                {"key", new Guid.NewGuid().ToString() },
                {"mUser", UserGUID().ToString()}, // get Members ID
    
            });
    
                Parent.SetValue("post", Newtonsoft.Json.JsonConvert.SerializeObject(uLikes));
    
     cs.SaveAndPublish(Parent);
    

    But the issue is every time it overwrites the Old Value with the new one. As I need to append/add new Items to nested content!

    so how can I do so?

    Thanks

    BJ

  • Neil Chapman 42 posts 170 karma points
    Aug 03, 2021 @ 09:06
    Neil Chapman
    1

    You are creating a new uLikes object every time and saving it.

    You need to get the existing uLikes object, deserialize it, add your new Dictionary item, and then save this updated uLikes value.

Please Sign in or register to post replies

Write your reply to:

Draft