Copied to clipboard

Flag this post as spam?

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


  • Edd Lynch 3 posts 37 karma points
    May 08, 2014 @ 12:19
    Edd Lynch
    1

    Create new content nodes programmatically

    Hi guys,

    We have recently upgraded to Umbraco 7. We are trying to develop a comment system where a user can write comments to a single blog post. The user completes a form and submits their comment. 

    Now we have the doc types set up for the comments. These comments are child nodes of the blog post doc. What we want is to be able to create new comment nodes whenever the user hits the submit button.

     

    The issue:-

    I have created a custom controller (inherits SurfaceController) and model (inherits RenderModel). I have passed the model into the blog view and by using the html helper I am able to display a form which contains inputs for each model property. Now when I fill out the form and hit the submit button nothing happens. A new node is NOT created and nothings gets added to the db.

    I have followed the Umbraco SurfaceContoller tutorial etc.

     

    Here is my custom controller:-

        public class uBlogsyPostSurfaceController : SurfaceController
    {
    
        public ActionResult uBlogsyPost()
        {
            return PartialView("~/Views/Partials/uBlogsy/Post/CommentsList.cshtml", new PostCommentsViewModel());
    
        }
    
        [HttpPost]
        [ActionName("uBlogsyPost")]
        public ActionResult uBlogsyPostNewComments(PostCommentsViewModel model)
        {
    
            return RedirectToCurrentUmbracoPage();
        }
    
    }
    

    }

    Here is my View:-

    @using (Html.BeginUmbracoForm("uBlogsyPost", "uBlogsyPostSurface"))
    
        @Html.EditorFor(x => Model)
        <input type="submit"  value="Submit"/> 
    

     Cheers.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    May 08, 2014 @ 12:58
    Jan Skovgaard
    101

    Hi Edd and welcome to our :)

    If you need to create a node based on some input from your front-facing website you should have a look at the API documentation here http://our.umbraco.org/documentation/Reference/Management-v6/Services/ContentService

    /Jan

  • Edd Lynch 3 posts 37 karma points
    May 08, 2014 @ 13:00
    Edd Lynch
    0

    Jan,

    Thank you for this and thanks for the quick response.

    Edd

  • Edd Lynch 3 posts 37 karma points
    May 08, 2014 @ 15:11
    Edd Lynch
    3

    Thank you this document was create. I was able to create new nodes from our website.

    Here is how I achieved this in case anyone else ran into the same issue:-

    Custom Controller:-

        public class uBlogsyPostSurfaceController : SurfaceController
    {
    
        public ActionResult uBlogsyPost()
        {
            return PartialView("~/Views/Partials/uBlogsy/Post/CommentsList.cshtml", new PostCommentsViewModel());
    
        }
    
        [HttpPost]
        [ActionName("uBlogsyPost")]
        public ActionResult uBlogsyPostNewComments(PostCommentsViewModel Model, int parentId)
        {
            ContentService c = new ContentService();
            createComment(Model.userName, Model.comments, parentId);
            return CurrentUmbracoPage();
        }
    
        private void createComment(string userName, string comment, int parentId)
        {
            int userId = 0;
            ContentService cs = new ContentService();
            var content = cs.CreateContent(DateTime.Today.ToString("yy/MM/dd"), parentId, "BlogComments", userId);
            content.SetValue("userName", userName);
            content.SetValue("comment", comment);
            cs.SaveAndPublishWithStatus(content);
        }
    
    }
    

    View:-

    @using (Html.BeginUmbracoForm("uBlogsyPost", "uBlogsyPostSurface")) {

        @Html.Hidden("parentId", "1269")
        @Html.EditorFor(x => Model)
        <input type="submit"  value="Submit"/> }
    
  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    May 08, 2014 @ 15:12
    Jan Skovgaard
    0

    Hi Edd

    Thanks for sharing! :) #h5yr

    /Jan

  • Calesire 4 posts 74 karma points
    Apr 27, 2017 @ 03:12
    Calesire
    0

    Sorry,but can you please tell me how to make umbraco 4 allow all these functions?

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Apr 30, 2017 @ 14:02
    Jan Skovgaard
    0

    Hi Calesire

    I'm afraid not - It seems that the old documentation for legacy Umbraco 4 installations have vanished over the years - I did a quick google search and can't seem to find any information about the old API's.

    Perhaps one can find something using the waybackmachine.org or by being very patient browsing through the results fond on Google.

    There are probably code samples etc. floating around on several blogs etc, which might enough to get you going - All of the above stuff is possible using the old API but without documentation it can of course be a bit hard to figure out when to do what etc.

    One blog that might be a good place to see if you can digg up something useful is http://www.nibble.be/?paged=18 - I set it to start at the very first page from 2008 - Try skimming through the post headlines and see if at some point there is something of interest - Tim has been doing a lot of nice stuff with the Umbraco API over the years - Hit the previous link in the bottom to see the newer posts. If you start by just going to the frontpage it will all be about Umbraco 7 and the new API's.

    Hope this can help a bit afterall?

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft