Copied to clipboard

Flag this post as spam?

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


  • Michael Sims 119 posts 387 karma points
    Aug 19, 2013 @ 22:50
    Michael Sims
    0

    CreateContent using razor

    I'm trying to create some content using razor. Everything seems to run fine and I'm not getting any exception but it is not creating any content. Any suggestions?

    try
    {
        // Create new content, the last param is the userId and is optional [default = 0]
    var newContent = Model.ContentService.CreateContent("My new document", nodeId, "Task");

    // Set property values
         newContent.SetValue("taskDuration", duration);
         newContent.SetValue("taskDate", date);
        newContent.SetValue("taskNotes", notes);

    // Save (or save and publish)
    Model.ContentService.SaveAndPublish(newContent);
    }
    catch (Exception ex)
    {
    @Html.Raw(ex.Message);
    }

  • andrew shearer 510 posts 659 karma points
    Aug 20, 2013 @ 04:26
    andrew shearer
    0

    looks fine to me. What is "nodeId" in the context of this example i.e. the parent content item you expect this new content to be added to?

    Also FYI, while there's no technical reason why you can't have this logic sitting in a razor view, it's better practice to have it in (or called from) a controller action.

Please Sign in or register to post replies

Write your reply to:

Draft