Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amirkhan81/b328bd7a6331ee0e2e9a4798d56c0b48 to your computer and use it in GitHub Desktop.
Save amirkhan81/b328bd7a6331ee0e2e9a4798d56c0b48 to your computer and use it in GitHub Desktop.
@using umbraco.cms.businesslogic.web
@* From https://gist.github.com/joeriks/877600
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment