Umbraco Create New Content in Postback

January 06, 2016

Umbraco's Back Office makes it easy to manage and create new content, but there may be specific data elements that may need to be created through the front-end application, ex. Comments on a Blog Post. You'll want to create the data type first within the Back Office. This is similar to creating the blueprint or Class structure for the entity, give it a name and a few properties. 

From within a SurfaceController, you'll have access to the powerful ContentService which will allow you to CreateContent. The parameters to this function is the alias for the data type, the parent node, and the name of the node. Next you'll want to set properties on the instance of the Data Type. Note that the 'model' below is a basic model object that closely resembles the Data Type that was setup in the Back Office, it's not the same thing. It is passed into the controller's action as a parameter.

public ActionResult PostComment(CommentsModel model) {
var newComment = Services.ContentService.CreateContent("comment", 1050, "Comment Name");
newComment.setValue("name", model.Name);
newComment.setValue("email", model.Email);
newComment.setValue("comment", model.Comment);
Services.ContentService.Save(newComment); // or SaveAndPublish(node)

public CommentsModel
  public string Name {get; set;}
public string Email {get; set;}
public string Comment {get; set;}