1

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

UPDATE:

Lee Kelleher pointed me in the right direction towards developing my own solution in this post on the umbraco forms. I hope to have time after this project to polish up my solution and submit a pull request to the project.

I basically ended up creating some extension methods that take an IEnumerable<IContent> and return a JSON representation of the objects for the NestedContent plugin.

Paul
  • 155
  • 14
  • A bit OT, but are you by chance trying to recreate Umbraco Forms style functionality? There's an open source alternative underway here: https://github.com/rhythmagency/formulate – Jannik Anker Apr 08 '16 at 17:24
  • @JannikAnker I wish it was that easy. In this project we have more specific requirements that unfortunately Umbraco Forms does not support. I wish they did but due to this we cannot use it...I wish we could. – Paul Apr 08 '16 at 19:15
  • Just in case it might be useful, Formulate is now live: http://www.formulate.rocks/ – Nicholas Westby Apr 11 '16 at 19:45

2 Answers2

0

It seems that the second argument of the SetPropertyValue method expects a string and you are passing a List<Umbraco.Core.Models.IContent>

Felipe Cruz
  • 1,119
  • 10
  • 18
  • I agree that appears to be the problem; however, when I look at the method signature of the **SetValue** method it is (string, object). Obviously that method is calling another method which has the signature (string, string). My suspicion is that I will need to serialize the NestedContent data into a JSON string but was hoping someone has come across this before and had a better tip. – Paul Apr 08 '16 at 19:34
  • You could take a look in the Umbraco source code (on Github) to confirm your suspicion? – Jannik Anker Apr 08 '16 at 19:49
  • I would try just reducing the fields and see which one of all of those are failing. For the moment add only one field: `newFormField.SetValue("fieldName", formField.Name);` We need to know in which property is failing (Name? Type? Manditory? Value?) – Felipe Cruz Apr 08 '16 at 19:51
  • The `SetPropertyValue` function makes a call to the `SetValueOnProperty(propertyTypeAlias, value);` function. As @JannikAnker pointed you can see the code here: https://github.com/umbraco/Umbraco-CMS/blob/75c2b07ad3a093b5b65b6ebd45697687c062f62a/src/Umbraco.Core/Models/ContentBase.cs#L450 – Felipe Cruz Apr 08 '16 at 19:57
0

This gist might help you:

robertjf/NestedContentCreator.cs

There's an example in the second file further down. It was written last year and may require some tweaking; but it should give you a good start.

Robert Foster
  • 2,317
  • 18
  • 28