Copied to clipboard

Flag this post as spam?

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


  • suzyb 458 posts 864 karma points
    Oct 26, 2017 @ 23:06
    suzyb
    0

    Creating Nested Content Programatically

    I'm trying to programmatically set a nested content property but am having a bit of trouble getting it working fully. The following code creates the nested content item on the document however none of the properties (title & bodyText) are set.

    Content doc = (Content)sender;
    
    IContentService contentService = ApplicationContext.Current.Services.ContentService;
    
    List<IContent> items = new List<IContent>();
    
    IContent newDesc = contentService.CreateContent("Description", doc.Id, "nestTextContent");
    newDesc.SetValue("title", "test");
    newDesc.SetValue("bodyText", cleanedDescription);
    
    items.Add(newDesc);
    
    doc.SetValue("pageContent", JsonConvert.SerializeObject(items));
    contentService.Save(doc);
    

    Looking at some older threads the answers say the value should be a "JSON serialized Dictionary<string, object> string" but I don't see what the contents of the dictionary should be.

    Am I barking up the wrong tree trying to serialise a list of IContents? If not how do I get the actual nested content item's properties set.

    These are the threads I've been referencing. https://our.umbraco.org/projects/backoffice-extensions/nested-content/nested-content-feedback/72977-can-i-write-collection-of-ipublishedcontent-back-to-model-property https://our.umbraco.org/projects/backoffice-extensions/nested-content/nested-content-feedback/76444-create-nestedcontent-items-in-surfacecontroller

  • David Parr 48 posts 179 karma points
    Oct 27, 2017 @ 07:34
    David Parr
    104

    Hi Suzy

    You can get this working, I hope the following code helps you out. You were close before, it is not a list of IContent you need though but a list of dictionary.

    Content doc = (Content)sender;
    
    IContentService contentService = ApplicationContext.Current.Services.ContentService;
    
    // Same as in your above example, title and body text values
    var items = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(doc.GetValue<string>("nestTextContent"));
    
    items.Add(new Dictionary<string, object>() {
      {
        "title",
        "test"
      },
      {
        "bodyText",
        cleanedDescription
      }
    });
    
    doc.SetValue("pageContent", JsonConvert.SerializeObject(items));
    contentService.Save(doc);
    

    This is just a basic working example for 7.3+ that I have tested.

  • Umbraco_Learner 2 posts 72 karma points
    Jul 12, 2019 @ 08:15
    Umbraco_Learner
    0

    hello

    I am trying to Set value and save a Nested Content programatically. Pretty much following the above steps.

    I get an error while Save - Object Reference not Set to an instance of an object.

    ApplicationContext.Current.Services.ContentService.Save(myContent);

    I am not sure why I get this.

  • suzyb 458 posts 864 karma points
    Oct 27, 2017 @ 09:19
    suzyb
    0

    Thanks so much David. I've got it working now.

    Obvious when it's pointed out but I just couldn't figure it out last night.

  • Cedric 15 posts 85 karma points
    Jun 05, 2018 @ 18:48
    Cedric
    0

    Hello I have a similar problem can you help me? I can not initialize items it tells me that the value is null and item.add logically I must put an object but I do not know which object thank you

    var mycontentService =  Services.ContentService;
            var itemregulatory = mycontentService.CreateContent(name, parentId, "regulatory",0);
            itemregulatory.SetValue("countryname", countryname);
            itemregulatory.SetValue("referTo", referTo);
            itemregulatory.SetValue("authorityName",authorityName);
            itemregulatory.SetValue("webSiteLink", webSiteLink);
            itemregulatory.SetValue("localTesting",localTesting);
            itemregulatory.SetValue("testsRequired", testsRequired);
            itemregulatory.SetValue("countryOwner",countryOwner);
            itemregulatory.SetValue("systemAproval", systemAproval);
            //itemregulatory.SetValue("antennaDependant",antennaDependant);
            itemregulatory.SetValue("sampleNeeded", sampleNeeded);
            itemregulatory.SetValue("countryInfoMarkingRequirements",countryInfoMarkingRequirements);
            itemregulatory.SetValue("agent", agent);
            itemregulatory.SetValue("certificationCost",certificationCost);
            //itemregulatory.SetValue("technology", "");
    
    
            //var items = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(itemregulatory.GetValue<string>("technology"));
            var items = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(itemregulatory.GetValue<string>("technology"));
    
            items.Add(new Dictionary<string, object>() {
              {
                "technologyUse1",
                "3G"
              },
              {
                "bandNumber",
                "002"
              }
            });
    
            itemregulatory.SetValue("technology", JsonConvert.SerializeObject(items));
            mycontentService.SaveAndPublishWithStatus(itemregulatory);
    
  • Sérgio 10 posts 101 karma points
    Aug 03, 2018 @ 16:44
    Sérgio
    0

    This line is probably return a null value as you don't have anything in the property yet:

    var items = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(itemregulatory.GetValue<string>("technology"));
    

    You need to check if items is null and, if so, initialize it accordingly:

    if (items == null) {
        items = new List<Dictionary<string, object>>();
    }
    
  • Stephen 767 posts 2263 karma points c-trib
    Apr 26, 2019 @ 07:27
    Stephen
    0

    Note: the serialized format of Nested Content property values has quite probably changed in v8. Here is what I have in database for a Nested Content property with only 1 TextBox:

    [
      {"key":"04a6dba8-813c-4144-8aca-86a3f24ebf08","name":"Item 1","ncContentTypeAlias":"nested","text":"woot"},
      {"key":"d8e214d8-c5a5-4b45-9b51-4050dd47f5fa","name":"Item 2","ncContentTypeAlias":"nested","text":"zoot"}
    ]
    

    So this will probably need to be amended for v8

  • ChristophC 16 posts 50 karma points
    29 days ago
    ChristophC
    0

    My solution in Umbraco 8.1.4:

    var liste = new List<Dictionary<string, string>>();
    
    Guid guid1 = Guid.NewGuid();
    
    liste.Add(new Dictionary<string, string>(){
        {"key","@guid1"},
        {"name","Some name"},
        {"ncContentTypeAlias","Your NestedContent Element-Type Alias"},
        {"Property in NestedContent","Some Text"},
    });
    
    node.SetValue("Your NestedContent Property Alias", JsonConvert.SerializeObject(liste));
    
Please Sign in or register to post replies

Write your reply to:

Draft