Copied to clipboard

Flag this post as spam?

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


  • Marshall Penn 79 posts 260 karma points
    Sep 22, 2020 @ 12:55
    Marshall Penn
    0

    Problem updating Nested Content programmatically

    I have added a new property - a Umbraco.DropDown.Flexible to a doctype thats used for nested content. The site has hundreds of instances of this nested content so i need to create a routine that sets the new property based one of the other properties (the title textstring). I loop through the published content of these instances, then create a new object and save the nested content property using contentservice. However while i am able to save textstrings, i cannot save complex datatypes. One of the properties is a Umbraco.MultiUrlPicker, and i cannot manage to pass that back in to the object to be saved.

    In the code below "columnType" is the new property, and "itemLink" is the Url Picker

    
    var nestedContentValue = new JArray();
    if (results != null && results.Any()) {
        foreach (var b in results) {
            var nestedContentItem = new JObject();
            nestedContentItem.Add("key", Guid.NewGuid().ToString());
            nestedContentItem.Add("ncContentTypeAlias", "resultItem");
            nestedContentItem.Add("itemTitle", b.GetPropertyValue<string>("itemTitle"));
            nestedContentItem.Add("columnType", "1335");
            nestedContentItem.Add("itemFilesizeText", b.GetPropertyValue<string>("itemFilesizeText"));
            nestedContentItem.Add("itemLink", JsonConvert.SerializeObject(b.GetProperty("itemLink")));
            nestedContentValue.Add(nestedContentItem);
        }
        var contentItem = cs.GetById(testnodeid);
        contentItem.SetValue("items", JsonConvert.SerializeObject(nestedContentValue));
        var saveresult = cs.SaveAndPublishWithStatus(contentItem);  
    }
    

    I have also tried the following approach, but this gave the same outcome: "itemLink" is not set

    
    dynamic ncItem = new ExpandoObject();
    var thelink = (IEnumerable<Link>)b.GetPropertyValue("itemLink");
    ((IDictionary<string, object>)ncItem).Add("name", "Some item name");
    ((IDictionary<string, object>)ncItem).Add("ncContentTypeAlias", "resultItem");
    ((IDictionary<string, object>)ncItem).Add("itemTitle", b.GetPropertyValue<string>("itemTitle") + " -@ ");
    ((IDictionary<string, object>)ncItem).Add("itemLink", thelink);
    ((IDictionary<string, object>)ncItem).Add("itemFilesizeText", b.GetPropertyValue<string>("itemFilesizeText"));
    ((IDictionary<string, object>)ncItem).Add("columnType", "1335");
    ncItems.Add(ncItem);
    

    Can anyone tell me the correct way to set these troublesome properties?

  • Marshall Penn 79 posts 260 karma points
    Sep 23, 2020 @ 08:50
    Marshall Penn
    101

    So i finally worked it out. I had to add the property in its json form:

    
    
    Link thelink = b.GetPropertyValue<Link>("itemLink");
    nestedContentItem.Add("itemLink", "[{\"name\": \"" + thelink.Name + "\",\"udi\": \"" + thelink.Udi + "\",\"url\": \"" + thelink.Url + "\", \"target\":\"" + thelink.Target + "\"}]");
    
    
Please Sign in or register to post replies

Write your reply to:

Draft