Copied to clipboard

Flag this post as spam?

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


  • Morten Peter Hagh Jensen 25 posts 85 karma points
    May 30, 2021 @ 08:14
    Morten Peter Hagh Jensen
    0

    Update nested content in nested content with ContentService

    I have a nested content list (meetingItemDayTimes) inside nested content list (meetingDatesAvailable).

    I want to set a checkbox to true on the each time item. But I can't for the life of me get it to work.

    The structure is like:

    enter image description here

    My code is this:

    var mainpage = contentService.GetById(1057);
    var dateItems = Umbraco.Content(parentNode).Value<IEnumerable<IPublishedElement>>("meetingDatesAvailable");
    var ncItems = new JArray();
    var ncDateItems = new JArray();
    var ncTimeItems = new JArray();
    
    if (dateItems != null && dateItems.Any())
    {
       foreach (var dateItem in dateItems)
       {
           var ncDateItem = new JObject();
           ncDateItem.Add("key", dateItem.Key.ToString());
           ncDateItem.Add("ncContentTypeAlias", dateItem.ContentType.Alias);
           ncDateItem.Add("meetingItemDay", dateItem.Value<string>("meetingItemDay", fallback: Fallback.ToDefaultValue, defaultValue: null));
    
           var times = dateItem.Value<IEnumerable<IPublishedElement>>("meetingItemDayTimes");
    
           foreach (var time in times)
           {
               var ncTimeItem = new JObject();
    
                            if (time.Key.ToString() == itemKey)
                            {
                                ncTimeItem.Add("meetingdateTimeItemUnavailable", true);
                            }
                            else
                            {
                                ncTimeItem.Add("meetingdateTimeItemUnavailable", time.Value<bool>("meetingdateTimeItemUnavailable", fallback: Fallback.ToDefaultValue, defaultValue: false));
                            }
    
                            ncTimeItem.Add("key", Guid.NewGuid().ToString());
                            ncTimeItem.Add("ncContentTypeAlias", time.ContentType.Alias);
                            ncTimeItem.Add("meetingdateTimeItem", time.Value<string>("meetingdateTimeItem", fallback: Fallback.ToDefaultValue, defaultValue: null));
                            ncTimeItems.Add(ncTimeItem);
                        }
    
                        ncDateItems.Add(ncDateItem);
                    }
    
                    ncItems.Add(ncDateItems);
    
                    var ncItemString = Newtonsoft.Json.JsonConvert.SerializeObject(ncItems);
                    mainpage.SetValue("meetingDatesAvailable", ncItemString);
                    contentService.SaveAndPublish(mainpage);
                    return new { saved = ncItemString };
                }
    

    I am really uncertain how to structure the list correctly.

Please Sign in or register to post replies

Write your reply to:

Draft