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?
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)
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:
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);
Comments...How to?
Hi,
How to add comments function ( like blog ) on a page?
How retrieve via Razor?
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
Ok...but is there already plugin? I see UComments.
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
Is it Razor compatible?
Hi Biagio,
I've made this before and here's what I did:
- 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)
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); } }
Hope it makes sense :-)
All the best,
Bo
Hi!
Here's a basic Razor approach to this, instructions in the code (https://gist.github.com/877600)
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?
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
Uhm...I try into 4.7. Seem that works.
I've rewrited Ucomment xslt for showing comments to Razor
is working on a reply...