Copied to clipboard

Flag this post as spam?

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


  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Mar 18, 2011 @ 14:49
    Biagio Paruolo
    0

    Comments...How to?

    Hi,

    How to add comments function ( like blog ) on a page?

    How retrieve via Razor?

  • Brad Hunt 26 posts 57 karma points
    Mar 18, 2011 @ 23:06
    Brad Hunt
    0

    One way to add the comments to a page would be to use a .NET control and create a simple comment form. Then create a doctype that has the needed properties (name, email address, comment) and then when the form is submitted, use the API to create a new instance of that doctype as a child of the page on which the form was placed. Does that help get you started?

    Brad

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Mar 19, 2011 @ 09:14
    Biagio Paruolo
    0

    Ok...but is there already plugin? I see UComments.

     

  • Brad Hunt 26 posts 57 karma points
    Mar 19, 2011 @ 14:27
    Brad Hunt
    0

    Yes- there is a project called ucomment and all the documentation, installation instructions, etc can be found here:

    http://our.umbraco.org/projects/collaboration/ucomment

    Brad

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Mar 19, 2011 @ 16:04
    Biagio Paruolo
    0

    Is it Razor compatible?

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Mar 19, 2011 @ 17:21
    Bo Damgaard Mortensen
    0

    Hi Biagio,

    I've made this before and here's what I did:

    1. Create two Document Types:
      - Comments (with no properties since this document type serves as a 'container' for comments by a given date)
      - Comment (with the properties: name, email, website, commentText)
    2. Created a UserControl to be able to write the actual comment. The code for this looks like this:
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            // Generate random number to prevent spam-bots flooding the site
                Random rng = new Random();
                int randomNumber = rng.Next(100);
                lblRng.Text = randomNumber.ToString();
            }
        }
    
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtAntiSpam.Text.Equals(lblRng.Text) && txtEmail.Text.Equals(txtValidateEmail.Text))
            {
                string currentDate = DateTime.Now.ToShortDateString();
                Node currentNode = Node.GetCurrent();
    
                Node childNode = null;
                bool found = false;
                int i = 0;
    
                while (!found && i < currentNode.Children.Count)
                {
                    if (currentNode.Children[i].Name.Equals(currentDate))
                    {
                        childNode = new Node(currentNode.Children[i].Id);
                        found = true;
                    }
                    i++;
                }
    
                if (childNode != null)
                {
                    DocumentType commentDocType = DocumentType.GetByAlias("Comment");
                    Document commentDoc = Document.MakeNew(currentDate + "_" + txtName.Text, commentDocType, new User(0), childNode.Id);
                    commentDoc.getProperty("navn").Value = txtName.Text;
                    commentDoc.getProperty("email").Value = txtEmail.Text;
                    commentDoc.getProperty("website").Value = txtWebsite.Text;
                    commentDoc.getProperty("kommentar").Value = txtComment.Text;
                    commentDoc.Save();
                    commentDoc.Publish(new User(0));
                    umbraco.library.RefreshContent();
                }
                else
                {
                    DocumentType docType = DocumentType.GetByAlias("Comments");
                    Document childDoc = Document.MakeNew(currentDate, docType, new User(0), currentNode.Id);
                    childDoc.Save();
                    childDoc.Publish(new User(0));
                    umbraco.library.RefreshContent();
    
                    DocumentType commentDocType = DocumentType.GetByAlias("Comment");
                    Document commentDoc = Document.MakeNew(currentDate + "_" + txtName.Text, commentDocType, new User(0), childDoc.Id);
                    commentDoc.getProperty("navn").Value = txtName.Text;
                    commentDoc.getProperty("email").Value = txtEmail.Text;
                    commentDoc.getProperty("website").Value = txtWebsite.Text;
                    commentDoc.getProperty("kommentar").Value = txtComment.Text;
                    commentDoc.Save();
                    commentDoc.Publish(new User(0));
                    umbraco.library.RefreshContent();
                    umbraco.library.UpdateDocumentCache(commentDoc.Id);
                }
                Response.Redirect(currentNode.Url);
            }
        }
    This UserControl checks if there's already a Comments node with the name/text equal to the the current date, if there is, it will create the new comment under this node. If there isn't, it will first create the Comments (container) node with the name/text set to the current date and after that, create the comment under this newly created Comments node.
    The above code makes the comment "module" generic because it basically make the Comments and/or Comment nodes as childnodes to the current page- you just need to allow these two document types to be created under the given node.
    Also, since it creates the Comments node first with the current date it makes it an easier workflow in the backend to delete old comments and/or find comments that will need to be edited. The result in Umbraco will look like this:

     

    Hope it makes sense :-)

     

    All the best,

    Bo

  • Jonas Eriksson 930 posts 1825 karma points
    Mar 19, 2011 @ 17:56
    Jonas Eriksson
    2

    Hi!

    Here's a basic Razor approach to this, instructions in the code (https://gist.github.com/877600)

    @using umbraco.cms.businesslogic.web

    @* A Razor script to add a basic comment function to Umbraco pages
    You need a document type called Comment with a textstring property
    called commentName and a textbox multiple property called commentMessage
    You also need to allow the Comment document type as child document type
    to your textpage document type.
    Create this script with a macro and add it below the bodyText in your
    template(s) *@

    @* Some global variables *@

    @{
    var successfulPost = false;
    var name = Request["name"];
    var message = Request["message"];
    }

    @* Handle form post: create a new document if a valid post*@

    @if (IsPost)
    {
    if (name!="" && message !="")
    {
    var dt = DocumentType.GetByAlias("Comment");

    @* Make sure its a valid document type alias *@

    if (dt != null)
    {

    var author = umbraco.BusinessLogic.User.GetUser(0);

    var doc = Document.MakeNew("Comment", dt, author, Model.Id);
    doc.getProperty("commentName").Value = name;
    doc.getProperty("commentMessage").Value = message;

    @* Tell umbraco to publish the document *@

    doc.Publish(author);
    umbraco.library.UpdateDocumentCache(doc.Id);

    successfulPost = true;
    }
    }
    }

    @* Render the Html *@

    <hr/>

    @if (successfulPost)
    {
    <h3>Successful post</h3>
    <p>Thank you <strong>@name</strong> for your message:</p>
    <p>@message</p>
    }

    <h3>Comments:</h3>

    @foreach (dynamic c in Model.Comment)
    {
    <div>
    <p>@c.commentMessage</p>
    <p>By : @c.commentName (at @c.CreateDate)</p>
    </div>
    }

    <h3>Add a new comment:</h3>

    <form method="post">
    <fieldset>
    <label for="name">Your name</label>
    <input type="text" name="name" id="name"/><br/>
    <label for="message">Comment</label>
    <textarea name="message" id="message"></textarea><br/>
    <input type="submit" value="Post comment"/>
    </fieldset>
    </form>
  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Mar 20, 2011 @ 12:13
    Biagio Paruolo
    0

    Hi, thank at all. I'll try.

    Razor approach is good because its not necessary a dll and it's possible to write code directly into Umbraco Backed.

    @Eriksson: Don't you use an antispam solution?

  • Jonas Eriksson 930 posts 1825 karma points
    Mar 20, 2011 @ 12:35
    Jonas Eriksson
    0

    Hi, yes, this was just a basic example as how to use the Razor to add comments. Here's a article describing adding "recaptcha" in a Razor script which should work pretty much the same in umbraco. http://ihatethissite.com/blog/2011/02/27/recaptcha-with-asp-net-mvc-razor-using-microsoft-web-helpers/

    Btw - did you try uComments ? I'd imagine it's pretty full featured.

    Hth

    Jonas

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Mar 20, 2011 @ 19:08
    Biagio Paruolo
    0

    Uhm...I try into 4.7. Seem that works.

  • Kouzzmitch 13 posts 36 karma points
    Oct 28, 2011 @ 18:50
    Kouzzmitch
    0

    I've rewrited Ucomment xslt for showing comments to Razor

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines
    @using UComment
    @using System.Xml.XPath
    
    @{
    
        var title = Model.Name;
        XPathNodeIterator nodesroot = XSLTLibrary.GetCommentsForNode(Model.Id);
        XPathNodeIterator nodes = nodesroot.Current.Select("//comment");
        int cnt = nodes.Count;
    
    }
    @functions {
    }
        @if (cnt > 0)
        {
            nodes.MoveNext();
            <h3>  "@title": @cnt .</h3>
            <ol class="commentlist">
            @for (int i = 0; i < cnt; i++)
            {
                
                XPathNavigator cur = nodes.Current;
                string url = cur.SelectSingleNode("./website").Value;
                string name = cur.SelectSingleNode("./name").Value;
                string email = cur.SelectSingleNode("./email").Value;
          
                <li class="comment alt" id="comment-@cur.GetAttribute("id","")">
                    <div class="comment-author vcard">
                        <img class="photo avatar avatar-32 photo" width="32" height="32" src="@UComment.XSLTLibrary.getGravatar(email40"")" alt="Gravatar of @name"/>
                        <span class="fn n">
                        @if (!String.IsNullOrWhiteSpace(url))
                        {@name}
                        else<a class="url url" rel="external nofollow" href="@url">@name</a>
                        }
                        </span>
                    </div>
                    <em class="comment-meta">
                         @umbraco.library.LongDate(cur.GetAttribute("created"""), true"  ")
                    </em>
                    <p>
                        @Html.Raw(umbraco.library.ReplaceLineBreaks(cur.SelectSingleNode("./message").InnerXml))
                    </p>
                </li>
    
                nodes.MoveNext();
                i++;
            }
            </ol>
        }
    
    
    
Please Sign in or register to post replies

Write your reply to:

Draft