Copied to clipboard

Flag this post as spam?

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


  • Paul Gower 28 posts 74 karma points
    Apr 07, 2016 @ 21:46
    Paul Gower
    1

    Create NestedContent Items In SurfaceController

    I have two document types:

    1. FormSubmission
    2. FormField

    The Form document type has a property named Fields which is a Nested Content data type that contains a list of FormField document types. I am trying to programmatically (in a SurfaceController) create a FormField and add it to the Fields property of the Form document type.

    Here is the code I am trying to use to do this:

    var newFormFields = new List<Umbraco.Core.Models.IContent>();
    int i = 0;
    foreach (var formField in model.Fields)
    {
        string fieldName = string.Format("Field {0}", i);
        var newFormField = contentService.CreateContent(fieldName, newFormSubmission.Id, "formFieldSubmission", formNode.CreatorId);
        newFormField.SetValue("fieldName", formField.Name);
        newFormField.SetValue("fieldType", formField.Type);
        newFormField.SetValue("manditory", formField.Manditory);
        newFormField.SetValue("fieldValue", formField.Value);
        newFormFields.Add(newFormField);
        ++i;
    }
    
    newFormSubmission.SetValue("fields", newFormFields);
    
    var status = contentService.SaveAndPublishWithStatus(newFormSubmission, formNode.CreatorId, raiseEvents: false);
    

    On the newFormSubmission.SetValue("fields", newFormFields); line it throws this exception:

    The best overloaded method match for 'Umbraco.Core.Models.ContentBase.SetPropertyValue(string, string)' has some invalid arguments

    Anyone have any ideas how to store a list of DocumentTypes in the Nested Content data type?

    PS: I am using Umbraco version 7.4.0 assembly: 1.0.5885.31226

  • Lee Kelleher 3945 posts 15163 karma points MVP 10x admin c-trib
    Apr 11, 2016 @ 09:47
    Lee Kelleher
    100

    Hi Paul,

    As you've seen, there's nothing built into Nested Content that enables you to programmatically set the property-value.

    I do quite like your code snippet... using a list of IContent does make sense!
    (We'd be totally open for a discussion/pull-request about adding an API to Nested Content for this - ping us via GitHub if you're interested.)


    Back to your question, currently the way to programmatically set the value is to have a JSON serialized Dictionary<string, object> string.

    I posted some hints/links on other forum posts:

    https://our.umbraco.org/projects/backoffice-extensions/nested-content/nested-content-feedback/73494-creating-new-elementsnodes

    https://our.umbraco.org/projects/backoffice-extensions/nested-content/nested-content-feedback/72977-can-i-write-collection-of-ipublishedcontent-back-to-model-property

    I hope this helps?

    Cheers,
    - Lee

  • Paul Gower 28 posts 74 karma points
    Apr 27, 2016 @ 20:54
    Paul Gower
    2

    Thanks for the help @leekelleher. I also submitted this question on stackoverflow.com here and I used the gist that Robert Foster posted in his answer as a guide to create the code necessary for my situation.

    I hope to have time after this project wraps up to create a pull request with the extension method I created.

    Thanks again for your response, @leekelleher!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies