Created
March 19, 2011 16:51
-
-
Save joeriks/877600 to your computer and use it in GitHub Desktop.
Basic Umbraco Razor commentary form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much for this script.
I had no idea there was such a simple way to let users add content to the node structure in umbraco.