Copied to clipboard

Flag this post as spam?

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


  • andrew shearer 440 posts 551 karma points
    Oct 11, 2018 @ 03:37
    andrew shearer
    0

    programmatically updating content in NestedContent by type

    Hello

    Does anybody have a convenient way of using the content service API to update content that resides in a NestedContent property?

    For example, say that a “Widget” doctype has been used to content load 100 pages in the website via NestedContent, how would you go about programmatically identifying the pages that use this Widget doctype in the NestedContent property so that you could then update a value within the Widget?

    The ContentService GetContentOfContentType doesn’t do this.

    thanks

  • Søren Kottal 571 posts 3749 karma points MVP 3x c-trib
    Oct 11, 2018 @ 07:02
    Søren Kottal
    3

    Hi Andrew

    You need to generate the JSON blob required by Nested Content yourself. It should be an array of objects, and each object must have the properties key, ncContentTypeAlias, and the properties of your desired document type.

    I have previously done like this

    var contentItem = ContentService.GetById(1);
    
    var nestedContentValue = new JArray();
    // you can add a loop here, if you need multiple nested items
    var nestedContentItem = new JObject();
    
    // you need to add a Guid as a key to your item
    nestedContentItem .Add("key", Guid.NewGuid().ToString());
    
    // and the desired doctype
    nestedContentItem.Add("ncContentTypeAlias", "myNestedType");
    
    // add the values for each property of your doctype
    nestedContentItem.Add("myPropertyAlias", "some property value");
    
    // finally, add you nestedContentItem, to your nestedContentValue
    nestedContentValue.Add(nestedContentItem);
    
    // then set the value of your IContent contentItem
    contentItem.SetValue("nestedContentProperty", JsonConvert.SerializeObject(nestedContentValue));
    

    Hope it helps :)

  • Jack Lawry 23 posts 145 karma points c-trib
    Oct 21, 2020 @ 13:22
    Jack Lawry
    0

    Hi Søren,

    Do you know if there is a way to update a just a single Nested Content record?

    I can save and replace all the NC items with the one I am saving but I am looking to just replace it based on its key?

    OR how you delete a single record from a list of nested content items?

    Thanks, Jack

  • andrew shearer 440 posts 551 karma points
    Oct 11, 2018 @ 19:24
    andrew shearer
    0

    thanks Søren :D

    i was just as interested to know how i should identify pages that contain the said doctype in their NestedContent property (your first line of code). Does anyone know convenient/efficient ways of doing that? thank you

  • Anzal 11 posts 81 karma points
    Oct 21, 2020 @ 17:34
    Anzal
    0
    var contentService = Current.Services.ContentService;
                var regulationNode = contentService.GetById(nodeID);
                var node = Umbraco.Content(nodeID);
                var downloadItems = node.Value<IEnumerable<IPublishedElement>>("addRegulationItems", culture: culture, fallback: Fallback.ToDefaultValue, defaultValue: null);
                var ncItems = new List<ListingItem>();
                int count = 0;
                try
                {
                    if (downloadItems != null && downloadItems.Any())
                    {
                        foreach (var content in downloadItems)
                        {
                            var ncItem = new ListingItem();
                            ncItem.title = content.Value<string>("title", culture: culture, fallback: Fallback.ToDefaultValue, defaultValue: null);
                            ncItem.description = content.Value<string>("description", culture: culture, fallback: Fallback.ToDefaultValue, defaultValue: null);
                            var downloadFile = content.GetProperty("downloadFile").GetSourceValue(culture: culture).ToString();
                            ncItem.downloadFile = downloadFile;
                            ncItem.uploadDate = content.Value<DateTime?>("uploadDate", culture: culture, fallback: Fallback.ToDefaultValue, defaultValue: null);
                            ncItem.key = content.Key.ToString();
                            ncItem.ncContentTypeAlias = content.ContentType.Alias;
                            var totalDownloads = content.Value<int>("totalDownloads");
                            if (ncItem.key == item)
                            {
                                if (totalDownloads > 0)
                                {
                                    count = totalDownloads + 1;
                                    ncItem.totalDownloads = count.ToString();
                                }
                                else
                                {
                                    count = 1;
                                    ncItem.totalDownloads = 1.ToString();
                                }
                            }
                            else
                            {
                                ncItem.totalDownloads = content.Value<string>("totalDownloads");
                            }
                            ncItems.Add(ncItem);
                        }
                        var ncItemString = Newtonsoft.Json.JsonConvert.SerializeObject(ncItems);
                        regulationNode.SetValue("addRegulationItems", ncItemString, culture: culture);
                        contentService.SaveAndPublish(regulationNode);
                        return Json(new { stat = true, count = count }, JsonRequestBehavior.AllowGet);
                    }
                }
    

    Hope this helps. You can fetch the key variable from each nested content element and update that speific element.

  • Jack Lawry 23 posts 145 karma points c-trib
    Oct 21, 2020 @ 19:19
    Jack Lawry
    0

    Thanks - I have managed to get is all working now.

  • Morten Peter Hagh Jensen 21 posts 71 karma points
    May 30, 2021 @ 07:24
    Morten Peter Hagh Jensen
    0

    How did you get this to work?

Please Sign in or register to post replies

Write your reply to:

Draft